add_compile_options 和 SET(CMAKE_CXX_FLAGS...) 的区别 [英] Difference between add_compile_options and SET(CMAKE_CXX_FLAGS...)
问题描述
这个问题与在驱动链接时指示Cmake使用CXX和CXXFLAGS有关?在前一个问题中,我们是试图指示 CMake 在调用链接器时使用 CXXFLAGS
.
This question is related to Instruct Cmake to use CXX and CXXFLAGS when driving link? In the former question, we are trying to instruct CMake to use CXXFLAGS
when it invokes the linker.
add_compile_options
我们发现下面的代码
if (CMAKE_VERSION VERSION_LESS 2.8.12)
add_definitions(-foo)
else()
add_compile_options(-foo)
endif()
message(STATUS, "CXXFLAGS: ${CMAKE_CXX_FLAGS}")
产生输出
CXXFLAGS:
设置 CMAKE_CXX_FLAGS
我们发现下面的代码
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -foo" )
message(STATUS, "CXXFLAGS: ${CMAKE_CXX_FLAGS}")
产生输出
CXXFLAGS: -foo
问题
我们发现 CMake 在这两种情况下都会使用 -foo
创建目标文件.所以 -foo
肯定会进入 CXXFLAGS
.
We found CMake would create object files using -foo
in both cases. So -foo
is definitely making its way into CXXFLAGS
.
第一组CMake代码和第二组CMake代码有什么区别?
What is the difference between the first set of CMake code and the second set of CMake code?
为什么 CMAKE_CXX_FLAGS
在一个实例中未设置,而在另一个实例中设置?
Why is CMAKE_CXX_FLAGS
unset in one instance, and set in the other instance?
推荐答案
CMAKE_CXX_FLAGS
用于为所有 C++ 目标添加标志.这对于传递一般参数(如警告级别)或选定的所需 C++ 标准非常方便.它对 C 或 Fortran 目标没有影响,用户可能会传递额外的标志.CMAKE_CXX_FLAGS
is used to add flags for all C++ targets. That's handy to pass general arguments like warning levels or to selected required C++ standards. It has no effect on C or Fortran targets and the user might pass additional flags.add_compile_options
将选项添加到目录及其子目录中的所有目标.如果您在目录中有一个库,并且您想向与该库相关但与所有其他目标无关的所有目标添加选项,这将非常方便.此外,add_compile_options
可以使用 生成器表达式.文档明确指出,add_compile_options
adds the options to all targets within the directory and its sub-directories. This is handy if you have a library in a directory and you want to add options to all the targets related to the library, but unrelated to all other targets. Additionally,add_compile_options
can handle arguments with generator expressions. The documentation explicitly states, that此命令可用于添加任何选项,但替代命令存在以添加预处理器定义 (
target_compile_definitions()
和add_definitions()
) 或包含目录(target_include_directories()
和include_directories()
).This command can be used to add any options, but alternative commands exist to add preprocessor definitions (
target_compile_definitions()
andadd_definitions()
) or include directories (target_include_directories()
andinclude_directories()
).add_definitions
旨在传递类型为-DFOO -DBAR=32
(Windows 上为/D
)的预处理器值,它定义了并设置预处理器变量.您可以传递任何标志,但会检测上述形式的标志并将其添加到[COMPILE_DEFINITIONS][2]
属性,您可以稍后阅读和更改该属性.在这里,您也可以使用生成器表达式.文档 提到了目录、目标和源文件的范围.add_definitions
is intended to pass pre-processor values of the type-DFOO -DBAR=32
(/D
on Windows) which defines and sets pre-processor variables. You could pass any flag, but the flags of the above form are detected and added to[COMPILE_DEFINITIONS][2]
property, which you can later read and change. Here, you can use generator expressions, too. The documentation mentions scopes for directories, targets and source files.
对于给定的目标,CMake 将从
CMAKE_CXX_FLAGS
、目标和目录的COMPILE_DEFINITIONS
以及所有影响目标的add_compile_options
收集所有标志.CMAKE_CXX_FLAGS
不会被其他命令改变,反之亦然.这将违反这些命令的范围.For a given target, CMake will collect all flags from
CMAKE_CXX_FLAGS
, the target's and directory'sCOMPILE_DEFINITIONS
and from alladd_compile_options
which affect the target.
CMAKE_CXX_FLAGS
are not altered by the other commands or vice versa. This would violate the scope of these commands.这篇关于add_compile_options 和 SET(CMAKE_CXX_FLAGS...) 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!