CMake:如何通过预处理器宏。

时间:2022-11-25 10:58:09

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_

Now I need to pass this value from CMake to the preprocessor. Setting a variable doesn't work, so how can I accomplish this?

现在我需要将这个值从CMake传递到预处理器。设置一个变量不起作用,那么我该怎么做呢?

2 个解决方案

#1


44  

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.

add_definition (-DCOMPILE_UNIT_TESTS) (cf. CMake的doc)或修改一个标记变量(CMAKE_CXX_FLAGS,或CMAKE_CXX_FLAGS_ )或设置目标上的COMPILE_FLAGS变量。

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.

同样,以下划线开头的标识符后面是一个大写字母,用于实现。包含双下划线的标识符。所以不要使用它们。

#2


15  

If you have a lot of preprocessor variables to configure, you can use configure_file:

如果要配置很多预处理器变量,可以使用configure_file:

Create a configure file, eg. config.h.in with

创建一个配置文件,如。config.h。在

#cmakedefine _COMPILE_UNIT_TESTS_
#cmakedefine OTHER_CONSTANT
...

then in your CMakeLists.txt:

然后在你的CMakeLists.txt:

set(_COMPILE_UNIT_TESTS_ ON CACHE BOOL "Compile unit tests") # Configurable by user 
set(OTHER_CONSTANT OFF) # Not configurable by user
configure_file(config.h.in config.h)

in the build directory, config.h is generated:

在构建目录中,配置。h是生成:

#define _COMPILE_UNIT_TESTS_
/* #undef OTHER_CONSTANT */

As suggested by robotik, you should add something like include_directories(${CMAKE_CURRENT_BINARY_DIR}) to your CMakeLists.txt for #include "config.h" to work in C++.

正如robotik所建议的,您应该向您的CMakeLists添加include_directory (${CMAKE_CURRENT_BINARY_DIR})之类的内容。txt # include”配置。在c++中工作。

#1


44  

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.

add_definition (-DCOMPILE_UNIT_TESTS) (cf. CMake的doc)或修改一个标记变量(CMAKE_CXX_FLAGS,或CMAKE_CXX_FLAGS_ )或设置目标上的COMPILE_FLAGS变量。

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.

同样,以下划线开头的标识符后面是一个大写字母,用于实现。包含双下划线的标识符。所以不要使用它们。

#2


15  

If you have a lot of preprocessor variables to configure, you can use configure_file:

如果要配置很多预处理器变量,可以使用configure_file:

Create a configure file, eg. config.h.in with

创建一个配置文件,如。config.h。在

#cmakedefine _COMPILE_UNIT_TESTS_
#cmakedefine OTHER_CONSTANT
...

then in your CMakeLists.txt:

然后在你的CMakeLists.txt:

set(_COMPILE_UNIT_TESTS_ ON CACHE BOOL "Compile unit tests") # Configurable by user 
set(OTHER_CONSTANT OFF) # Not configurable by user
configure_file(config.h.in config.h)

in the build directory, config.h is generated:

在构建目录中,配置。h是生成:

#define _COMPILE_UNIT_TESTS_
/* #undef OTHER_CONSTANT */

As suggested by robotik, you should add something like include_directories(${CMAKE_CURRENT_BINARY_DIR}) to your CMakeLists.txt for #include "config.h" to work in C++.

正如robotik所建议的,您应该向您的CMakeLists添加include_directory (${CMAKE_CURRENT_BINARY_DIR})之类的内容。txt # include”配置。在c++中工作。