在 add_compile_options()
的手册页中,我没有看到任何关于如何修改发布/调试编译器标志的提及。 您可以使用 add_compiler_options()
修改发布/调试编译器标志吗?如果是,如何?
如果不是,推荐的规范方法是修改 the release/debug cmake variables[1] as described here 吗?
[1]
即设置 cmake 变量 CMAKE_
最佳答案
如果你想在你的几个项目中重用你的编译器设置,或者你需要区分 C 和 C++ 的编译器选项,我会推荐 CMAKE_C_FLAGS
/CMAKE_CXX_FLAGS
带有 toolchain file 的变体对于每个支持的编译器(参见例如 here 或 here)。
但是如果您只需要在您的项目中添加一些额外的 C++ 编译器选项,请使用 add_compile_options()
, target_compile_options()
或 target_compile_features()
是要走的路。
是的,您可以在此处区分 DEBUG
和 RELEASE
。
示例
add_compile_options()
命令确实需要 generator expressions :add_compile_options("$<$<CONFIG:DEBUG>:/MDd>")
或
add_compile_options( "$<$<CONFIG:RELEASE>:-std=gnu99>" "$<$<CONFIG:DEBUG>:-std=gnu99 -g3>" )
最好同时检查编译器 ID:
add_compile_options("$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:DEBUG>>:/MDd>")
或
if (MSVC) add_compile_options("$<$<CONFIG:DEBUG>:/MDd>") endif()
让 CMake 决定更好 the correct compiler options为你。所以你可以设置
CXX_STANDARD
您的目标需要:set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
或给出 compiler feature您的目标需求
target_compile_features()
add_library(mylib requires_constexpr.cpp) # cxx_constexpr is a usage-requirement target_compile_features(mylib PUBLIC cxx_constexpr)
引用资料
关于c++ - 对于 Cmake,您可以使用 `add_compiler_flags()` 命令修改发布/调试编译器标志吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33829152/