简体   繁体   English

如何在CMake中只更改一个可执行文件的编译器标志?

[英]How to change a compiler flag for just one executable in CMake?

I have a CMake project that supports multiple processor compilation in Visual Studio through the \\MP flag. 我有一个CMake项目,它通过\\MP标志支持Visual Studio中的多处理器编译。

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? 由于在项目构建的许多可执行文件中只有一个,我需要将\\MP标志设置为false(或禁用它,因为我输入.tlb文件时出错),如何设置此目标的标志不同?

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? 我应该将一些set_target_properties给cmake还是专门从整个项目中删除该标志? Thank you! 谢谢!

You can use set_source_files_properties to add COMPILE_FLAGS for myprogram.cpp. 您可以使用set_source_files_properties添加COMPILE_FLAGS为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 : 如果MyProgram目标中的所有源文件都需要这些标志,则可以将set_target_properties与目标属性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

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 一个包含所有带有\\ MP标志的源文件,另一个包含myprogram.cpp

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: 在我的情况下,我想删除启用RTTI,因为它默认是禁用的:

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! 这就像是CMake 3的魅力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM