如何使用 CMake 通过命令行定义 C++ 预处理器宏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8564337/
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
How to define a C++ preprocessor macro through the command line with CMake?
提问by ybungalobill
I try to set a preprocessor macro in the command line of CMake. I've tried:
我尝试在 CMake 的命令行中设置预处理器宏。我试过了:
set generator="Visual Studio 8 2005"
set params=-D MY_MACRO=1
cmake.exe -G %generator% %params% ..\some_project
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
在 CMake 生成的文件中找到名称,除了CMakeCache.txt
它在表单中的位置:
MY_MACRO:UNINITIALIZED=1
How can I do it?
我该怎么做?
采纳答案by ybungalobill
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:
问题背后的动机是批量构建 3rd 方库,这就是我想避免修改 CMakeLists.txt 的原因。多年后,即使我不再需要它,我发现它可以通过CMake 外部的方式轻松实现:
Invoke CMake as usual, no special flags.
Then:
With MSVC:The compiler reads the
CL
environment variable to get extra command line arguments. Soset CL=/DMY_MACRO=1 %CL%
then invoke MSBuild to do its job.
With other toolchains:It might be doable by setting
CFLAGS
orCXXFLAGS
environment variables prior to callingmake
, but I didn't check that.
像往常一样调用 CMake,没有特殊标志。
然后:
使用 MSVC:编译器读取
CL
环境变量以获取额外的命令行参数。所以set CL=/DMY_MACRO=1 %CL%
然后调用 MSBuild 来完成它的工作。
使用其他工具链:在调用 之前通过设置
CFLAGS
或CXXFLAGS
环境变量可能是可行的make
,但我没有检查。
回答by freitass
A good alternative would be to define a cmake option:
一个不错的选择是定义一个 cmake 选项:
OPTION(DEFINE_MACRO "Option description" ON) # Enabled by default
Followed by a condition:
后跟一个条件:
IF(DEFINE_MACRO)
ADD_DEFINITIONS(-DMACRO)
ENDIF(DEFINE_MACRO)
Then you can turn that option ON/OFF via command line with cmake using the -D
flag. Example:
然后,您可以通过命令行使用 cmake 使用-D
标志打开/关闭该选项。例子:
cmake -DDEFINE_MACRO=OFF ..
To make sure the compiler is receiving the definition right, you can call make in verbose mode and check for the macro being defined or not:
为了确保编译器正确接收定义,您可以在详细模式下调用 make 并检查是否定义了宏:
make VERBOSE=1
This is a good solution also because make
will recompile your code when any of cmake options changes.
这也是一个很好的解决方案,因为make
当任何 cmake 选项更改时都会重新编译您的代码。
回答by SlavaNov
Try this: -D CMAKE_CXX_FLAGS=/DMY_MACRO=1
尝试这个: -D CMAKE_CXX_FLAGS=/DMY_MACRO=1
回答by hiobs
Unless you have a good reason not to, you should use ADD_DEFINITIONS(<name>=<value>[, ...])
.
除非您有充分的理由不这样做,否则您应该使用ADD_DEFINITIONS(<name>=<value>[, ...])
.
Just add the following line to your CMakeLists.txt:
只需将以下行添加到您的 CMakeLists.txt 中:
ADD_DEFINITIONS("MY_MACRO=1")
CMake will take care of the syntax of the switches (be it -D<name>=<value>
, or /D<name>=<value>
).
CMake 将处理开关的语法(无论是-D<name>=<value>
,还是/D<name>=<value>
)。