在 CMake 中设置通用编译标志的现代方法是什么? [英] What is the modern method for setting general compile flags in 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_
是一个全局变量,使用起来最容易出错.它也不支持 生成器表达式,这可以派上用场.
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_OPTIONS
和 INTERFACE_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}>")
PUBLIC
、PRIVATE
和 INTERFACE
关键字定义了 选项的范围.例如,如果我们使用 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 targetbar
PUBLIC
options will be applied to both, the original targetfoo
and the consuming targetbar
这篇关于在 CMake 中设置通用编译标志的现代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!