CMake:为#cmakedefine变量设置不同的名称 [英] CMake: Set different name for #cmakedefine variable

查看:315
本文介绍了CMake:为#cmakedefine变量设置不同的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用CMake的 configure_file 使CMake变量可用于您的程序。例如,我可以使用

I know you can use CMake's configure_file to make CMake variables available to your program. For example, I can use

#define ${CMAKE_BUILD_TYPE}

导致

#define Release

但是,为了使我的代码更易读,我希望定义

However, to keep my code more readible, I would prefer to define

#define BUILD_TYPE_RELEASE

有没有简单的方法可以实现

Is there a simple way to achieve this?

推荐答案

这更多是您首选编程风格(配置文件与编译器定义)的问题。

This is more a question of your preferred programming style (configuration files vs. compiler definitions).

在您的情况下,我将使用 add_definitions() 或直接修改/附加 COMPILE_DEFINITIONS 目录属性(使用生成器表达式还支持多配置环境):

In your case I would use add_definitions() or directly modify/append COMPILE_DEFINITIONS directory property (using generator expressions also supports multi-configuration environments):

set_property(
    DIRECTORY 
    APPEND 
    PROPERTY 
        COMPILE_DEFINITIONS "BUILD_TYPE_$<UPPER_CASE:$<CONFIG>>"
)

最简化的调试/发布检查

您还可以检查CMake预定义了哪些编译器定义。无需修改/添加到您的 CMakeLists.txt 文件中,您只需检查 NDEBUG 定义(为Release设置)即可。在C / C ++代码中构建跨平台)

You can also check what compiler definitions CMake does pre-define. Without having to modify/add something to your CMakeLists.txt files you could simply check for NDEBUG definition (set for Release builds across platforms) in you C/C++ code:

#ifdef NDEBUG
... 
#endif

参考

  • CMake: How to pass preprocessor macros
  • How to check if a CMake build directory build type is Debug or Release?
  • What predefined macro can be used to detect debug build with clang?

这篇关于CMake:为#cmakedefine变量设置不同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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