C ++的CMake命令行#define [英] CMake command line for C++ #define

查看:1249
本文介绍了C ++的CMake命令行#define的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过添加编译器开关来编译某个项目的不同版本。通常,我会使用add_definitions或类似

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

set_property( TARGET mylib PROPERTY COMPILE_DEFINITIONS _MYDEFINE=1 )

在CMakeLists.txt文件中。

in the CMakeLists.txt file.

在此特定项目中但是,我不允许修改任何源,包括CMakeLists.txt 文件。

In this specific project however, I am not allowed to modify any sources, including the CMakeLists.txt file.

我希望有类似的东西

cmake -D_MYDEFINE=1 <path to sources>

将生成一个项目文件(在我的情况下为Visual Studio 2008,但不要紧),其中包括_MYDEFINE = 1在其预处理程序定义中,但实际上不会。

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.

我在这里有什么选择?是否有其他cmake命令行选项来实现这一目标?只要不需要更改项目的CMakeLists.txt,就可以建议不包括命令行的解决方案。

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.

推荐答案

I现在设法做到了这一点:

I managed to do it this way now:

我能够说服每个人在通用的CMakeLists.txt中添加以下行:

I was able to convince everybody to add the following lines to the common CMakeLists.txt:

IF (NOT DEFINED _MYDEFINE)
    SET(_MYDEFINE <default value>)
ENDIF()
ADD_DEFINITIONS(-D_MYDEFINE=${_MYDEFINE})

(不,它不是真正的 MYDEFINE,并且<默认值> ;只是一个占位符,我只替换了本例中的所有内容)。

(No it is not really called "MYDEFINE", and <default value> is just a placeholder, I just replaced all that for this example)

这不会更改当前的编译行为,而没有其他编译器标志,因此是有效的更改。

This does not change the current behaviour of compiling with no additional compiler flags and is thus a valid change.

它允许您执行

cmake -D_MYDEFINE=<my value> <path to sources>

其中,当cmake创建项目文件时,该cmake定义将映射到C ++预编译器定义。

where this cmake definition will be mapped to a C++ precompiler definition when cmake creates the project file.

这篇关于C ++的CMake命令行#define的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆