如何创建一个C#定义某个目标使用CMake? [英] How to create a C #define for a certain target using CMake?

查看:170
本文介绍了如何创建一个C#定义某个目标使用CMake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在觉得有点蠢。最近转换一些较小的项目使用CMake,我决定也摆脱几个Platform_Config.h文件。这些文件包含一些预处理指令,如 #define USE_NEW_CACHE 和控制编译。

I feel a little stupid right now. After recently converting a few smaller projects to use CMake, I decided to also get rid of a few "Platform_Config.h" files. These files contain a few preprocessing directives like #define USE_NEW_CACHE and control compilation.

定义用CMake控制?理想情况下,通过使用这些缓存变量,用户可以轻松地编辑。

How would I 'convert' these defines to be controlled with CMake? Ideally by using these "cache" variables the user can easily edit.

推荐答案

您可以使用 add_definitions 方法将定义传递为编译器标记:例如

There are two options. You can use the add_definitions method to pass defines as compiler flags: E.g. somewhere in your projects cmakelists.txt:

add_definitions( -DUSE_NEW_CACHE )

CMake将确保-D前缀转换为您的编译器的正确标志(/ D为msvc和-D为gcc)。

CMake will make sure the -D prefix is converted to the right flag for your compiler (/D for msvc and -D for gcc).

或者,请查看 configure_file 它更复杂,但可能更适合您的原始方法与Platform_Config文件。

Alternatively, check out configure_file. It is more complex, but may be better suited to your original approach with a Platform_Config file.

您可以创建一个类似于原始Platform_Config.h的输入文件,并向其中添加#cmakedefine行。

You can create an input-file, similar to your original Platform_Config.h and add "#cmakedefine" lines to it.

让我们在Platform_Config.h.in中调用:

Let's call in Platform_Config.h.in:

// In Platform_Config.h.in
#cmakedefine USE_NEW_CACHE
// end of Platform_Config.h.in

当时执行

configure_file( ${CMAKE_SOURCE_DIR}/Platform_Config.h.in ${CMAKE_BINARY_DIR}/common/Platform_Config.h )

它将在build-dir中生成一个新的Platform_Config文件。 cmake中也是cmakedefine的那些变量将出现在生成的文件中,其他变量将被注释掉或未定义。

it will generate a new Platform_Config file in your build-dir. Those variables in cmake which are also a cmakedefine will be present in the generated file, the other ones will be commented out or undefed.

当然,您应该确保实际的,生成的文件,然后正确找到它包括在您的源文件中。

Of course, you should make sure the actual, generated file is then correctly found when including it in your source files.

这篇关于如何创建一个C#定义某个目标使用CMake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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