16

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

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

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

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

linello
  • 8,451
  • 18
  • 63
  • 109
  • Related, see [Override compile flags for single files](https://stackoverflow.com/q/13638408/608639). – jww Aug 23 '17 at 09:13
  • Possible duplicate of [CMake - remove a compile flag for a single translation unit](https://stackoverflow.com/questions/28344564/cmake-remove-a-compile-flag-for-a-single-translation-unit) – Cris Luengo Oct 05 '18 at 17:58

2 Answers2

18

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 those flags for all source files in the MyProgram target, you could 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 a single property, you can first get all the properties and manually remove the offending flag from the list. For example with 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 in two. One with all the source files with the \MP flag and another with only myprogram.cpp

André
  • 18,348
  • 6
  • 60
  • 74
  • 1
    Apologies, I misread your question and thought you wanted to set properties specifically for a single file or target. I will update my answer... – André Jun 16 '14 at 08:50
  • An empty string replacement didn't work for me (CMake error : "set_source_files_properties called with incorrect number of arguments"), I had to use a blank space : STRING( REPLACE "/MP1" " " MYPROPS ${MYPROPS} ) – anno Nov 05 '14 at 11:09
  • 1
    Do you know how to avoid the warning after it has been replaced: `D9025 : overriding '/GR-' with '/GR'` – jaques-sam Apr 19 '19 at 09:05
4

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 removing the flag from the default compiler flags first, than set it to your target. In my case I wanted to remove enable RTTI 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}

This works like a charm with CMake 3!

jaques-sam
  • 2,578
  • 1
  • 26
  • 24