C++ 通过 CMake 定义预处理器宏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9017573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define preprocessor macro through CMake?
提问by Mythli
How do I define a preprocessor variable through CMake?
如何通过 CMake 定义预处理器变量?
The equivalent code would be #define foo
.
等效的代码是#define foo
.
回答by ypnos
For a long time, CMake had the add_definitions
command for this purpose. However, recently the command has been superseded by a more fine grained approach (separate commands for compile definitions, include directories, and compiler options).
很长一段时间以来,CMake 都有add_definitions
用于此目的的命令。但是,最近该命令已被更细粒度的方法(用于编译定义、包括目录和编译器选项的单独命令)取代。
An example using the new add_compile_definitions:
使用新add_compile_definitions的示例:
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION})
add_compile_definitions(WITH_OPENCV2)
Or:
或者:
add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2)
The good part about this is that it circumvents the shabby trickery CMake has in place for add_definitions
. CMake is such a shabby system, but they are finally finding some sanity.
这样做的好处在于它规避了 CMake 为add_definitions
. CMake 是一个如此破旧的系统,但他们终于找到了一些理智。
Find more explanation on which commands to use for compiler flags here: https://cmake.org/cmake/help/latest/command/add_definitions.html
在此处找到有关用于编译器标志的命令的更多说明:https: //cmake.org/cmake/help/latest/command/add_definitions.html
Likewise, you can do this per-target as explained in Jim Hunziker's answer.
同样,您可以按照 Jim Hunziker 的回答中的说明按目标执行此操作。
回答by Jim Hunziker
To do this for a specific target, you can do the following:
要针对特定目标执行此操作,您可以执行以下操作:
target_compile_definitions(my_target PRIVATE FOO=1 BAR=1)
You should do this if you have more than one target that you're building and you don't want them all to use the same flags. Also see the official documentation on target_compile_definitions.
如果您要构建的目标不止一个,并且您不希望它们都使用相同的标志,那么您应该这样做。另请参阅有关target_compile_definitions的官方文档。
回答by Leos313
The other solution proposed on this page are useful some versions of Cmake <
3.3.2
. Here the solution for the version I am using (i.e.,3.3.2
). Check the version of your Cmake by using$ cmake --version
and pick the solution that fits with your needs. The cmake documentationcan be found on the official page.
此页面上提出的其他解决方案对某些版本的 Cmake <
3.3.2
很有用。这里是我使用的版本的解决方案(即,3.3.2
)。通过使用$ cmake --version
并选择适合您需求的解决方案来检查您的 Cmake 的版本。该cmake的文档可在官方网页上找到。
With CMake version 3.3.2, in order to create
使用 CMake 3.3.2 版本,为了创建
#define foo
I needed to use:
我需要使用:
add_definitions(-Dfoo) # <--------HERE THE NEW CMAKE LINE inside CMakeLists.txt
add_executable( ....)
target_link_libraries(....)
and, in order to have a preprocessor macro definition like this other one:
并且,为了有一个像另一个这样的预处理器宏定义:
#define foo=5
the line is so modified:
该行如此修改:
add_definitions(-Dfoo=5) # <--------HERE THE NEW CMAKE LINE inside CMakeLists.txt
add_executable( ....)
target_link_libraries(....)