通过CMake定义预处理器宏? [英] Define preprocessor macro through CMake?

查看:442
本文介绍了通过CMake定义预处理器宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过CMake定义预处理器变量?

How do I define a preprocessor variable through CMake?

等效代码为 #define foo

推荐答案

很长一段时间,CMake为此使用了 add_definitions 命令目的。但是,最近该命令已被更精细的方法(用于编译定义,包含目录和编译器选项的单独命令)所取代。

For a long time, CMake had the add_definitions command for this purpose. However, recently the command has been superseded by a more fine grained approach (separate commands for compile definitions, include directories, and compiler options).

使用新的< a href = https://cmake.org/cmake/help/latest/command/add_compile_definitions.html#command:add_compile_definitions rel = noreferrer> add_compile_definitions :

add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION})
add_compile_definitions(WITH_OPENCV2)

或:

add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2)

这方面的好处是,它避免了CMake为 add_definitions 。 CMake是一个破旧的系统,但是他们终于找到了一些理智。

The good part about this is that it circumvents the shabby trickery CMake has in place for add_definitions. CMake is such a shabby system, but they are finally finding some sanity.

在此处查找有关用于编译器标志的命令的更多说明: https://cmake.org/cmake/help/latest/command/add_definitions.html

Find more explanation on which commands to use for compiler flags here: https://cmake.org/cmake/help/latest/command/add_definitions.html

同样,您可以按照Jim Hunziker的答案中的说明按目标进行此操作。

Likewise, you can do this per-target as explained in Jim Hunziker's answer.

这篇关于通过CMake定义预处理器宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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