且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Cmake:为多配置 cmake 项目指定配置特定设置

更新时间:2021-12-12 02:52:47

在 CMake 中,用于多配置构建工具的配置特定设置的常用方法是使用 生成器表达式.

In CMake common way for config-specific settings for multi-config build tools is using generator expressions.

Command add_library 允许对源文件使用生成器表达式.例如.这个:

Command add_library allows to use generator expressions for source files. E.g. this:

add_library(mylib common.c $<$<CONFIG:DEBUG>:debug.c>)

创建一个由所有配置中的 common.c 加上 Debug 配置中的附加 debug.c 组成的库.

creates a library consisted from common.c in all configuration plus additional debug.c in Debug configuration.

add_definitions 的文档没有说明生成器的用法表达式,但 target_compile_definitions 的文档:

Documentation for add_definitions doesn't note usage of generator expressions, but documentation for target_compile_definitions does:

target_compile_definitions(mylib PUBLIC $<$<CONFIG:DEBUG>:-DDEBUG_VAR>)