CMake中的set_target_properties是否覆盖CMAKE_CXX_FLAGS? [英] Does set_target_properties in CMake override CMAKE_CXX_FLAGS?
问题描述
在我的CMake项目的开始,我在变量CMAKE_CXX_FLAGS中设置一般编译标志,如
At the beginning of my CMake project, I'm setting general compilation flags in the variable CMAKE_CXX_FLAGS, like
set(CMAKE_CXX_FLAGS "-W -Wall ${CMAKE_CXX_FLAGS}")
特定的编译标志(存储在BUILD_FLAGS中)。我可以为此使用以下命令:
Later on, I need to append additional configuration-specific compilation flags (stored in BUILD_FLAGS). Can I use the following command for this:
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})
或者我必须手动添加CMAKE_CXX_FLAGS:
or do I have to add the CMAKE_CXX_FLAGS manually:
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BUILD_FLAGS}")
以防止CMAKE_CXX_FLAGS被BUILD_FLAGS覆盖?
to prevent CMAKE_CXX_FLAGS being overriden by BUILD_FLAGS?
推荐答案
使用第一个:
set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})
在编译TARGET的源时,存储在BUILD_FLAGS中的标志附加在CMAKE_CXX_FLAGS之后。
The flags stored in BUILD_FLAGS are appended after CMAKE_CXX_FLAGS when compiling the sources of TARGET. The documentation hints at this, but I've just tried it to make sure.
COMPILE_FLAGS
COMPILE_FLAGS
Additional flags to use when compiling this target's sources.
The COMPILE_FLAGS property sets additional compiler flags used to
build sources within the target. Use COMPILE_DEFINITIONS to
pass additional preprocessor definitions.
完整的命令行将等效于:
The full command line will be the equivalent of:
${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS} -o foo.o -c foo.cc
正如Ramon所说,你可以随时用检查,使VERBOSE = 1
。
And as Ramon said, you can always check with make VERBOSE=1
.
这篇关于CMake中的set_target_properties是否覆盖CMAKE_CXX_FLAGS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!