mongodb 如何在cmake中添加宏的定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6770834/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:10:45  来源:igfitidea点击:

How to add macro's definition in cmake?

boostmongodbcmake

提问by Dean Chen

I am using Mongodb client and Boost in my C++ application. Because the Mongodb client is still using Boost old filesystem and my C++ application is using filesystem version 3 from boost 1.47.0, they conflict.

我在我的 C++ 应用程序中使用 Mongodb 客户端和 Boost。因为 Mongodb 客户端仍在使用 Boost 旧文件系统,而我的 C++ 应用程序使用的是 boost 1.47.0 中的文件系统版本 3,所以它们会发生冲突。

I found a way to solve this compilation problem, namely add a macro definition before all include statements for the header files from Boost in my cpp files:

我找到了解决这个编译问题的方法,即在我的 cpp 文件中所有来自 Boost 的头文件的 include 语句之前添加一个宏定义:

#define BOOST_FILESYSTEM_VERSION 2

But I want to know how to put the above macro's definition into my CMake project files.

但我想知道如何将上述宏的定义放入我的 CMake 项目文件中。

回答by André

Take a look at add_definitions, which will add your definitions to your compiler command line, e.g. -Dwith gcc, or /Dwith MSVC. Try something like:

看看add_definitions,它会将您的定义添加到您的编译器命令行,例如-D使用 gcc 或/DMSVC。尝试类似:

add_definitions( -DBOOST_FILESYSTEM_VERSION=2 )

In your case, I would definitely go with the add_definitionmethod, but an alternative may to take a look at configure_file. Then you can create a header-file template, which will be filled with cmake-values and include this in your source files. This can be useful if you have many, many configurable parameters which are determined by CMake.

在您的情况下,我肯定会采用该add_definition方法,但另一种方法可能是查看configure_file。然后您可以创建一个头文件模板,该模板将填充 cmake-values 并将其包含在您的源文件中。如果您有很多由 CMake 确定的可配置参数,这会很有用。