问题描述
在 add_compile_options()
的手册页中,我没有提到如何修改Release / Debug编译器标志。 可以使用 add_compiler_options()
来修改Release / Debug编译器标志吗?如果是,怎么办?
如果不是,建议的规范方法是修改?
[1]
,即设置cmake变量CMAKE_< LANG> _FLAGS_< TYPE> (对于lang c / c ++,它将是:CMAKE_CXX_FLAGS_RELEASE,CMAKE_CXX_FLAGS_DEBUG,CMAKE_C_FLAGS_RELEASE,CMAKE_C_FLAGS_DEBUG)。
通过几个项目的编译器设置,或者需要在C和C ++之间进行区分,我建议使用 CMAKE_C_FLAGS
/ CMAKE_CXX_FLAGS $ c带有的$ c>变体对于每个受支持的编译器(请参见例如或 )。
但是如果您只需要在项目中使用一些其他C ++编译器选项,请使用,或是一种解决方法。
是的,您可以在其中区分 DEBUG
和 RELEASE
。
示例
-
The
add_compile_options()
命令确实需要: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决定。因此,您可以设置:
或输入您的目标需要
参考
In the man page for add_compile_options()
I don't see any mention of how to modify the Release/Debug compiler flags. Can you use add_compiler_options()
to modify the Release/Debug compiler flags? If yes, how?
If no, is the recommended canonical method to modify the release/debug cmake variables[1] as described here ?
[1]i.e. set the cmake variables CMAKE_<LANG>_FLAGS_<TYPE> (for lang c/c++ it would be: CMAKE_CXX_FLAGS_RELEASE, CMAKE_CXX_FLAGS_DEBUG, CMAKE_C_FLAGS_RELEASE, CMAKE_C_FLAGS_DEBUG).
If you want to reuse your compiler settings through several of your projects or you need to differentiate the compiler options between C and C++, I would recommend the CMAKE_C_FLAGS
/CMAKE_CXX_FLAGS
variant with a toolchain file for each of your supported compilers (see e.g. here or here).
But if you just need some additional C++ compiler options in your project, taking add_compile_options()
, target_compile_options()
or target_compile_features()
is the way to go.
And yes, you can differentiate between DEBUG
and RELEASE
there.
Examples
The
add_compile_options()
command does take generator expressions:add_compile_options("$<$<CONFIG:DEBUG>:/MDd>")
or
add_compile_options( "$<$<CONFIG:RELEASE>:-std=gnu99>" "$<$<CONFIG:DEBUG>:-std=gnu99 -g3>" )
Better to check also the compiler id:
add_compile_options("$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:DEBUG>>:/MDd>")
or
if (MSVC) add_compile_options("$<$<CONFIG:DEBUG>:/MDd>") endif()
Even better to let CMake decide the correct compiler options for you. So you can either set the
CXX_STANDARD
needed for your target:or give the compiler feature your target needs with
target_compile_features()
References
- CMake CMAKE_CXX_FLAGS enabled optimization unexpectly
- Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake
- CMake generator expression, differentiate C / C++ code
这篇关于对于Cmake,可以使用`add_compiler_flags()`命令修改发布/调试编译器标志吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!