How to change the compiler flag for only one executable file in CMake?

I have a CMake project that supports multiple processor compilation in Visual Studio using the \MP flag.

Since only in one of the many executable files that build the project, I need to set the \MP flag to false (or disable it because I get errors importing the .tlb file), how can I set the flags for this purpose is different?

 add_executable(MyProgram myprogram.cpp) target_link_libraries(MyProgram MyLibraries) 

Should I give some set_target_properties for cmake or specifically remove the flag from the whole project? Thank!

+10
c ++ compiler-construction visual-studio-2010 cmake
Jun 16 '14 at 7:54
source share
2 answers

You can use set_source_files_properties to add COMPILE_FLAGS for myprogram.cpp. For example:

 add_executable(MyProgram myprogram.cpp) # Add the -std=c++11 flag as an example set_source_files_properties( myprogram.cpp PROPERTIES COMPILE_FLAGS "-std=c++11" ) target_link_libraries(MyProgram MyLibraries) 

If you need these flags for all source files in the MyProgram target, you can use set_target_properties with the target property COMPILE_FLAGS :

 add_executable(MyProgram myprogram.cpp) # Add the -std=c++11 flag as an example target_link_libraries(MyProgram MyLibraries) set_target_properties( MyProgram PROPERTIES COMPILE_FLAGS "-std=c++11" ) 

Update: to remove one property, you can first get all the properties and manually remove the offender flag from the list. For example, using get_source_file_property :

 get_source_file_property( MYPROPS myprogram.cpp COMPILE_FLAGS ) STRING( REPLACE "/MP1" "" MYPROPS ${MYPROPS} ) set_source_files_properties( myprogram.cpp COMPILE_FLAGS ${MYPROPS} ) 

However, I would recommend splitting your source files into two parts. One with all the source files with the \ MP flag, and the other with myprogram.cpp only

+10
Jun 16 '14 at 8:28
source share

New approach

 # Simply add the opposite flag to the target if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") target_compile_options(${TARGET_NAME} PRIVATE "/GR") else() target_compile_options(${TARGET_NAME} PRIVATE "-frtti") # works even if -fno-rtti is set to CXX_FLAGS endif() 

Old approach:

You can disable it by first removing the flag from the default compiler flags, than setting it to your purpose. In my case, I wanted to remove the RTTI permission because it was disabled by default:

 function(enable_RTTI target_name) if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(NO_RTTI "/GR-") set(WITH_RTTI "/GR") else() set(NO_RTTI "-fno-rtti") endif() string(REPLACE "${NO_RTTI}" "${WITH_RTTI}" COMPILE_FLAGS_RTTI_ENABLED "${CMAKE_CXX_FLAGS}") set_target_properties(${target_name} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS_RTTI_ENABLED}") endfunction() ... # Do this on your specific target enable_RTTI(${TARGET_NAME} 

It works like a charm with CMake 3!

0
Apr 19 '19 at 9:38
source share



All Articles