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
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:
SET CMAKE_CXX_FLAGS
SET CMAKE_CXX_FLAGS
我们发现以下代码
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -foo" )
message(STATUS, "CXXFLAGS: ${CMAKE_CXX_FLAGS}")
产生输出
CXXFLAGS: -foo
问题
Questions
我们发现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类型的预处理器值code>(在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屋!
-