C++ 的 CMake 命令行 #define
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9531637/
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
CMake command line for C++ #define
提问by Tim Meyer
I need to compile different versions of a certain project by adding compiler switches. Usually I would do this by using add_definitions or something like
我需要通过添加编译器开关来编译某个项目的不同版本。通常我会通过使用 add_definitions 或类似的东西来做到这一点
set_property( TARGET mylib PROPERTY COMPILE_DEFINITIONS _MYDEFINE=1 )
in the CMakeLists.txt file.
在 CMakeLists.txt 文件中。
In this specific project however, I am not allowed to modify any sources, including the CMakeLists.txtfile.
但是,在这个特定项目中,我不允许修改任何来源,包括 CMakeLists.txt文件。
I was hoping that something like
我希望像
cmake -D_MYDEFINE=1 <path to sources>
would generate a project file (Visual Studio 2008 in my case, but shouldn't matter) which includes _MYDEFINE=1 in its preprocessor definitions but in fact it won't.
将生成一个项目文件(在我的情况下为 Visual Studio 2008,但应该无关紧要),其中在其预处理器定义中包含 _MYDEFINE=1 但实际上它不会。
What are my options here? Is there a different cmake command line option to achieve this? Feel free to suggest solutions not including the command line, as long as changing the project's CMakeLists.txt is not necessary.
我在这里有哪些选择?是否有不同的 cmake 命令行选项来实现这一点?只要不需要更改项目的 CMakeLists.txt,就可以随意建议不包括命令行的解决方案。
采纳答案by Tim Meyer
I managed to do it this way now:
我现在设法这样做了:
I was able to convince everybody to add the following lines to the common CMakeLists.txt:
我能够说服每个人将以下几行添加到通用的 CMakeLists.txt 中:
IF (NOT DEFINED _MYDEFINE)
SET(_MYDEFINE <default value>)
ENDIF()
ADD_DEFINITIONS(-D_MYDEFINE=${_MYDEFINE})
(No it is not really called "MYDEFINE", and <default value> is just a placeholder, I just replaced all that for this example)
(不,它并没有真正被称为“MYDEFINE”,并且 <default value> 只是一个占位符,我只是在这个例子中替换了所有这些)
This does not change the current behaviour of compiling with no additional compiler flags and is thus a valid change.
这不会改变没有额外编译器标志的当前编译行为,因此是一个有效的更改。
And it allows you to do
它允许你做
cmake -D_MYDEFINE=<my value> <path to sources>
where this cmake definition will be mapped to a C++ precompiler definition when cmake creates the project file.
当 cmake 创建项目文件时,此 cmake 定义将映射到 C++ 预编译器定义。
回答by Naszta
Container CMakeLists.txt
solution
集装箱CMakeLists.txt
解决方案
Tricky solution:
棘手的解决方案:
Your read only CMakeList.txt
path: ${path}/ReadOnlyProject/CMakeLists.txt
您的只读CMakeList.txt
路径:${path}/ReadOnlyProject/CMakeLists.txt
Create a new CMakeList.txt
to upper to the read only library (${path}/CMakeLists.txt
):
新建一个CMakeList.txt
只读库(${path}/CMakeLists.txt
):
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0)
PROJECT (FAKE_PROJECT)
ADD_DEFINITIONS(-D_MYDEFINE=1)
ADD_SUBDIRECTORY(ReadOnlyProject)
Now use your new project (FAKE_PROJECT
) to compile. If the ReadOnlyProject
does not set compilers definitions directly, it could work.
现在使用您的新项目 ( FAKE_PROJECT
) 进行编译。如果ReadOnlyProject
不直接设置编译器定义,它可以工作。
On Visual Studio 2010:
在 Visual Studio 2010 上:
Try to modify c:\Users\${username}\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props
to add custom compiler settings.
尝试修改c:\Users\${username}\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props
以添加自定义编译器设置。
You should add the followings:
您应该添加以下内容:
<Project>
...
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>__MYDEFINE=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
</Project>
回答by sakra
To pass a C++ or C pre-processor define without modifying any CMake source files, use the environment variables CFLAGS
for C or CXXFLAGS
for C++ respectively, e.g.:
要在不修改任何 CMake 源文件的情况下传递 C++ 或 C 预处理器定义,请分别使用CFLAGS
C 或CXXFLAGS
C++的环境变量,例如:
$ export CXXFLAGS="-D_MY_DEFINE=1 -D_MY_OTHER_DEFINE=1"
$ mkdir build
$ cd build
$ cmake ..