CMake:如何传递预处理器宏 [英] CMake: How to pass preprocessor macros
问题描述
如何将宏传递给预处理器?例如,如果我想编译我的代码的一部分,因为用户想要编译单元测试,我会这样做:
How can I pass a macro to the preprocessor? For example, if I want to compile some part of my code because a user wants to compile unit test, I would do this:
#ifdef _COMPILE_UNIT_TESTS_
BLA BLA
#endif //_COMPILE_UNIT_TESTS_
需要将此值从CMake传递给预处理器。设置一个变量不起作用,那么我该如何实现呢?
Now I need to pass this value from CMake to the preprocessor. Setting a variable doesn't work, so how can I accomplish this?
推荐答案
add_definitions(-DCOMPILE_UNIT_TESTS )
(cf. CMake的文档) 修改标记变量( CMAKE_CXX_FLAGS
或 CMAKE_CXX_FLAGS_< configuration>
)或在目标上设置 COMPILE_FLAGS
变量。
add_definitions(-DCOMPILE_UNIT_TESTS)
(cf. CMake's doc) or modify one of the flag variables (CMAKE_CXX_FLAGS
, or CMAKE_CXX_FLAGS_<configuration>
) or set COMPILE_FLAGS
variable on the target.
后跟一个大写字母保留用于实现。包含双下划线的标识符。所以不要使用它们。
Also, identifiers that begin with an underscore followed by an uppercase letter are reserved for the implementation. Identifiers containing double underscore, too. So don't use them.
这篇关于CMake:如何传递预处理器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!