在 CMake 中设置通用编译标志的现代方法是什么? [英] What is the modern method for setting general compile flags in CMake?

查看:22
本文介绍了在 CMake 中设置通用编译标志的现代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMake 提供了多种机制来向编译器获取标志:

There are multiple mechanisms offered by CMake for getting flags to the compiler:

在现代使用中,是否有一种方法比另一种方法更受欢迎?如果是为什么?另外,这种方法如何与MSVC等多个配置系统一起使用?

Is there one method that is preferred over the other in modern use? If so why? Also, how can this method be used with multiple configuration systems such as MSVC?

推荐答案

对于现代 CMake(2.8.12 及更高版本),您应该使用 target_compile_options,在内部使用目标属性.

For modern CMake (versions 2.8.12 and up) you should use target_compile_options, which uses target properties internally.

CMAKE__FLAGS 是一个全局变量,使用起来最容易出错.它也不支持 生成器表达式,这可以派上用场.

CMAKE_<LANG>_FLAGS is a global variable and the most error-prone to use. It also does not support generator expressions, which can come in very handy.

add_compile_options 基于目录属性,这在某些情况下很好,但通常不是指定选项的最自然方式.

add_compile_options is based on directory properties, which is fine in some situations, but usually not the most natural way to specify options.

target_compile_options 在每个目标的基础上工作(通过设置 COMPILE_OPTIONSINTERFACE_COMPILE_OPTIONS 目标属性),这通常会产生最干净的 CMake 代码,因为源文件的编译选项取决于文件所属的项目(而不是它放在硬盘上的哪个目录中).这有一个额外的好处,它 如果需要,会自动处理将选项传递给依赖目标.

target_compile_options works on a per-target basis (through setting the COMPILE_OPTIONS and INTERFACE_COMPILE_OPTIONS target properties), which usually results in the cleanest CMake code, as the compile options for a source file are determined by which project the file belongs to (rather than which directory it is placed in on the hard disk). This has the additional advantage that it automatically takes care of passing options on to dependent targets if requested.

尽管它们有点冗长,但每个目标的命令允许对不同的构建选项进行合理的细粒度控制,并且(根据我的个人经验)从长远来看是最不可能引起头痛的.

Even though they are little bit more verbose, the per-target commands allow a reasonably fine-grained control over the different build options and (in my personal experience) are the least likely to cause headaches in the long run.

理论上,你也可以直接使用set_target_properties来设置各自的属性,但是target_compile_options通常更具可读性.

In theory, you could also set the respective properties directly using set_target_properties, but target_compile_options is usually more readable.

例如,要根据使用生成器表达式的配置设置目标 foo 的编译选项,您可以编写:

For example, to set the compile options of a target foo based on the configuration using generator expressions you could write:

target_compile_options(foo PUBLIC "$<$<CONFIG:DEBUG>:${MY_DEBUG_OPTIONS}>")
target_compile_options(foo PUBLIC "$<$<CONFIG:RELEASE>:${MY_RELEASE_OPTIONS}>")

PUBLICPRIVATEINTERFACE 关键字定义了 选项的范围.例如,如果我们使用 target_link_libraries(bar foo)foo 链接到 bar:

The PUBLIC, PRIVATE, and INTERFACE keywords define the scope of the options. E.g., if we link foo into bar with target_link_libraries(bar foo):

  • PRIVATE 选项将只应用于目标本身 (foo),而不应用于链接到它的其他库(消费者).
  • INTERFACE 选项将只应用于消费目标 bar
  • PUBLIC 选项将应用于原始目标 foo 和消费目标 bar
  • PRIVATE options will only be applied to the target itself (foo) and not to other libraries (consumers) linking against it.
  • INTERFACE options will only be applied to the consuming target bar
  • PUBLIC options will be applied to both, the original target foo and the consuming target bar

这篇关于在 CMake 中设置通用编译标志的现代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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