cmake的四個命令:add_compile_options、add_definitions、target_compile_definitions、build_commandc++
add_compile_options()express
Adds options to the compilation of source files.app
增長源文件的編譯選項。ide
add_compile_options(<option> ...)
Adds options to the compiler command line for targets in the current directory and below that are added after this command is invoked. See documentation of the directory and target COMPILE_OPTIONS properties.ui
爲當前路徑和下層路徑的目標增長編譯器命令行選項,選項在此命令被調用後添加。查看文檔中關於路徑和目標的 COMPILE_OPTIONS 屬性。this
This command can be used to add any options, but alternative commands exist to add preprocessor definitions (target_compile_definitions() and add_definitions()) or include directories (target_include_directories() and include_directories()).命令行
這個命令能夠被用來添加任何的選項,可是存在替代命令(target_compile_definitions() 和 add_definitions())增長預處理定義或(target_include_directories() 和 include_directories())包含路徑。c++11
Arguments to add_compile_options may use 「generator expressions」 with the syntax $<...>. See the cmake-generator-expressions(7) manual for available expressions. See the cmake-buildsystem(7) manual for more on defining buildsystem properties.orm
add_compile_options的參數能夠使用帶語法$<...>的「生成表達式」。關於有效的表達式能夠查看cmake-generator-expressions(7)手冊。關於更多的系統屬性的定義能夠查看cmake-buildsystem(7)助手。ci
add_definitions()
Adds -D define flags to the compilation of source files.
爲源文件的編譯添加由-D定義的標誌。
add_definitions(-DFOO -DBAR ...)
Adds definitions to the compiler command line for targets in the current directory and below (whether added before or after this command is invoked). This command can be used to add any flags, but it is intended to add preprocessor definitions (see the add_compile_options() command to add other flags). Flags beginning in -D or /D that look like preprocessor definitions are automatically added to the COMPILE_DEFINITIONS directory property for the current directory. Definitions with non-trivial values may be left in the set of flags instead of being converted for reasons of backwards compatibility. See documentation of the directory, target, source file COMPILE_DEFINITIONS properties for details on adding preprocessor definitions to specific scopes and configurations.
爲當前路徑以及下層路徑的目標加入編譯器命令行定義(定義在命令調用以前或以後被添加,注:也就是不肯定)。這個命令能夠用來添加任何標誌,可是它的原意是用來增長預處理器的定義(查看 add_compile_options() 命令增長其它的定義)。那些以 -D 或 /D 開頭的標誌,看起來像預處理器定義的flag,會被自動加到當前路徑的 COMPILE_DEFINITIONS 屬性中。爲了後向兼容,非簡單值(non-trival,指的是什麼?——譯註)的定義會被留在flags組(flags set)裏,而不會被轉換。關於在特定的域以及配置中增長預處理器的定義,參考路徑、目標以及源文件的 COMPILE_DEFINITIONS 屬性來獲取更多的細節。
See the cmake-buildsystem(7) manual for more on defining buildsystem properties.
關於更多的系統屬性的定義能夠查看cmake-buildsystem(7)助手。
target_compile_definitions()
Add compile definitions to a target.
爲目標增長編譯定義。
target_compile_definitions(<target> <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...] )
Specify compile definitions to use when compiling a given <target>. The named <target> must have been created by a command such as add_executable() or add_library() and must not be an Imported Target.
編譯給定的 <target> 時使用指定的編譯定義。<target> 必須是 add_executable() 或者 add_library() 建立的,而且不是一個輸入目標。
The INTERFACE, PUBLIC and PRIVATE keywords are required to specify the scope of the following arguments. PRIVATE and PUBLIC items will populate the COMPILE_DEFINITIONS property of <target>. PUBLIC and INTERFACE items will populate the INTERFACE_COMPILE_DEFINITIONS property of <target>. The following arguments specify compile definitions. Repeated calls for the same <target> append items in the order called.
關鍵字INTERFACE,PUBLIC和PRIVATE用來指定其後參數的做用域。PRIVATE 和 PUBLIC 項將產生 <target> 的 COMPILE_DEFINITIONS 屬性。PUBLIC 和 INTERFACE 項將產生 <target> 的INTERFACE_COMPILE_DEFINITIONS 屬性。其後的參數指定編譯定義。重複調用相同的目標將按照調用順序追加(定義)。
Arguments to target_compile_definitions may use 「generator expressions」 with the syntax $<...>. See the cmake-generator-expressions(7) manual for available expressions. See the cmake-buildsystem(7) manual for more on defining buildsystem properties.
target_compile_definitions的參數能夠使用帶語法$<...>的「生成表達式」。關於有效的表達式能夠查看cmake-generator-expressions(7)手冊。關於更多的系統屬性的定義能夠查看cmake-buildsystem(7)助手。
build_command()
Get a command line to build the current project. This is mainly intended for internal use by the CTest module.
獲取構建該工程的命令行。一般是供CTest模塊的內部使用。
注:筆者給出了一個簡單的例子在文檔結尾。
build_command(<variable> [CONFIGURATION <config>] [TARGET <target>] [PROJECT_NAME <projname>] # legacy, causes warning )
Sets the given <variable> to a command-line string of the form:
<cmake> --build . [--config <config>] [--target <target>] [-- -i]
where <cmake> is the location of the cmake(1) command-line tool, and <config> and <target> are the values provided to the CONFIGURATION and TARGET options, if any. The trailing -- -i option is added for Makefile Generators if policy CMP0061 is not set to NEW.
When invoked, this cmake --build command line will launch the underlying build system tool.
build_command(<cachevariable> <makecommand>)
This second signature is deprecated, but still available for backwards compatibility. Use the first signature instead.
It sets the given <cachevariable> to a command-line string as above but without the --target option. The <makecommand> is ignored but should be the full path to msdev, devenv, nmake, make or one of the end user build tools for legacy invocations.
Note In CMake versions prior to 3.0 this command returned a command line that directly invokes the native build tool for the current generator. Their implementation of the PROJECT_NAME option had no useful effects, so CMake now warns on use of the option.
example
cmake_minimum_required(VERSION 2.8) project(cmaketest) #set(CMAKE_CXX_COMPILER "g++") add_compile_options(-std=c++11 -w) #add_definitions(-std=c++11) build_command(BUILD_COMMAND_LINE CONFIGURATION ${CMAKE_BUILD_TYPE} PROJECT_NAME cmaketest TARGET all) message("build command:${BUILD_COMMAND_LINE}") message("using compiler ${CMAKE_CXX_COMPILER}") add_executable(test main.cpp)
build command:/usr/bin/make -i "all"
using compiler /usr/bin/c++
// main.cpp int main(int argc, char *argv[]) { int n = 5.5f; auto func = [&](int n) {return n < 5;}; return 0; }