CMAKE 中 add_definitions的用法.

9 篇文章 1 订阅

1.官方的说明

Adds -D define flags to the compilation of source files.

add_definitions(-DFOO -DBAR ...)

Adds definitions to the compiler command line for sources in the current directory and below. This command can be used to add any flags, but it is intended to add preprocessor definitions. Flags beginning in -D or /D that look like preprocessor definitions are automatically added to the COMPILE_DEFINITIONS directory property for the current directory. Definitions with non-trivial values may be left in the set of flags instead of being converted for reasons of backwards compatibility. See documentation of the directorytargetsource file COMPILE_DEFINITIONS properties for details on adding preprocessor definitions to specific scopes and configurations.

https://cmake.org/cmake/help/v3.0/command/add_definitions.html2.

2.小例子:

假设项目是以CMakeLists.txt 构建的.

代码中通过宏 USE_MACRO 作为区分.

...

#ifdef USE_MACRO

...

#endif

我们可以通过在项目中的CMakeLists.txt 中添加如下代码控制代码的开启和关闭.

 

+ OPTION(USE_MACRO

+  "Build the project using macro"

+  OFF)

+ IF(USE_MACRO)

+  add_definitions("-DUSE_MACRO")

+ endif(USE_MACRO)

 

 

3.运行构建项目的时候可以添加参数控制宏的开启和关闭.

 

开启: cmake -DUSE_MACRO=on ..

关闭: cmake -DUSE_MACRO=off ..

  • 14
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值