如何使用CMake通过命令行定义C ++预处理器宏? [英] How to define a C++ preprocessor macro through the command line with CMake?
问题描述
我尝试在CMake的命令行中设置预处理器宏。我尝试过:
I try to set a preprocessor macro in the command line of CMake. I've tried:
set generator="Visual Studio 8 2005"
set params=-D MY_MACRO=1
cmake.exe -G %generator% %params% ..\some_project
但是它在编译时既没有定义,也没有在CMake生成的文件中找到名称 MY_MACRO
,除了 CMakeCache.txt $ c之外$ c>的形式:
but it's neither defined when I compile nor can I find the name MY_MACRO
in the files generated by CMake at all, except for CMakeCache.txt
where it's present in the form:
MY_MACRO:UNINITIALIZED=1
我该怎么办?
推荐答案
问题背后的动机是批量构建第三方库,这就是为什么我要避免修改CMakeLists的原因。这么多年后,即使我不再需要它了,我也发现可以通过 CMake外部的手段轻松实现:
The motivation behind the question was to batch build 3rd party libraries, which is why I wanted to avoid modifying CMakeLists. So years later, even though I don't need that anymore, I figured out that it's easily achievable by means external to CMake:
-
照常调用CMake,没有特殊标志。
Invoke CMake as usual, no special flags.
然后:
-
使用MSVC:编译器读取
CL
环境变量以获取额外的命令行参数。因此
With MSVC: The compiler reads the
CL
environment variable to get extra command line arguments. So
set CL=/DMY_MACRO=1 %CL%
然后调用MSBuild来完成其工作。
then invoke MSBuild to do its job.
与其他工具链:通过在调用 make之前设置
CFLAGS
或 CXXFLAGS
环境变量可能是可行的code>,但我没有检查。
With other toolchains: It might be doable by setting CFLAGS
or CXXFLAGS
environment variables prior to calling make
, but I didn't check that.
这篇关于如何使用CMake通过命令行定义C ++预处理器宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!