CMake传递相关宏至源代码

本文介绍了如何通过CMake在编译时传递参数,并利用这些参数设置预处理宏。CMake提供了-D选项更新CACHE变量,使用add_compile_definitions添加预处理器定义。通过示例展示了不同编译命令下程序对预处理宏的响应。
摘要由CSDN通过智能技术生成

问题

有时我们需要根据Cmake编译时传入不同参数,运行我们不同分支的代码,用于支持不同场景,那么该如何解决?

解决思路

1、如何通过Cmake的编译命令传入参数?
cmake提供如下机制进行传递变量:

-D <var>:<type>=<value>, -D <var>=<value>
Create or update a CMake CACHE entry.

When CMake is first run in an empty build tree, it creates a CMakeCache.txt file and populates it with customizable settings for the project. This option may be used to specify a setting that takes priority over the project’s default value. The option may be repeated for as many CACHE entries as desired.

If the : portion is given it must be one of the types specified by the set() command documentation for its CACHE signature. If the : portion is omitted the entry will be created with no type if it does not exist with a type already. If

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值