CMAKE 中 add_definitions的用法.
1.官方的说明
Adds -D define flags to the compilation of source files.
add_definitions(-DFOO -DBAR ...)add_definitions(-DFOO -DBAR ...)
Adds definitions to the compiler command line for sources in the current directory and below. This command can be used to add any flags, but it is intended to add preprocessor definitions. Flags beginning in -D or /D that look like preprocessor definitions are automatically added to the directory property for the current directory. Definitions with non-trivial values may be left in the set of flags instead of being converted for reasons of backwards compatibility. See documentation of the , , COMPILE_DEFINITIONS properties for details on adding preprocessor definitions to specific scopes and configurations.
https://cmake.org/cmake/help/v3.0/command/add_definitions.html2.
2.小例子:
假设项目是以CMakeLists.txt 构建的.
代码中通过宏 USE_MACRO 作为区分.
...
#ifdef USE_MACRO
...
#endif
我们可以通过在项目中的CMakeLists.txt 中添加如下代码控制代码的开启和关闭.
+ OPTION(USE_MACRO + "Build the project using macro" + OFF) + IF(USE_MACRO) + add_definitions("-DUSE_MACRO") + endif(USE_MACRO)
3.运行构建项目的时候可以添加参数控制宏的开启和关闭.
开启: cmake -DUSE_MACRO=on ..
关闭: cmake -DUSE_MACRO=off ..