How to change a compiler flag for just one executable in CMake?(如何在 CMake 中仅更改一个可执行文件的编译器标志?)
问题描述
我有一个 CMake 项目,它通过 MP
标志在 Visual Studio 中支持多处理器编译.
I have a CMake project that supports multiple processor compilation in Visual Studio through the MP
flag.
由于只是项目构建的众多可执行文件之一,我需要将 MP
标志设置为 false(或禁用它,因为导入 .tlb 时出错
文件),我怎样才能为这个目标设置不同的标志?
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)
我应该给 cmake 一些 set_target_properties
还是专门从整个项目中删除标志?谢谢!
Should I give some set_target_properties
to cmake or specifically remove the flag from the whole project?
Thank you!
推荐答案
可以使用set_source_files_properties 添加 COMPILE_FLAGS 用于 myprogram.cpp.例如:
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)
如果您需要 MyProgram 目标中所有源文件的这些标志,您可以使用 set_target_properties 与目标属性 COMPILE_FLAGS:
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" )
更新:要删除单个属性,您可以先获取所有属性,然后手动从列表中删除违规标志.例如 get_source_file_property:
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} )
但是,我建议将源文件一分为二.一个包含所有带有 MP 标志的源文件,另一个包含 myprogram.cpp
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
这篇关于如何在 CMake 中仅更改一个可执行文件的编译器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 CMake 中仅更改一个可执行文件的编译器标志?
- 静态初始化顺序失败 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- C++ 协变模板 2021-01-01