I. Overview
1, CMake role
CMake is a cross-platform, open-source build system. It is a set of software building, testing, packing and one software. For example, you can build a profile build of Visual Studio .sln files in the Makefile or Linux based on the source code and pre-configured, start the compilation process start unit testing, packaging, executable files and depend to a specified directory.
2, CMake advantage
- Cross-platform support for Windows, Linux, Mac, Android system;
- Scalability, and support for control logic syntax, conditional compilation;
Two, CMake example of Helloworld
CMake build process configuration items by CMakeLists.txt file, which is usually located in the root directory of the project. Other file formats as well xxx.cmake, similar to the function of such files in the C ++ header file, may be include into the CMakeLists.txt to scale the configuration of the build.
1, the project directory, configuration CMakeLists.txt
- Create a src directory that contains hello.cpp file;
- The root directory is created CMakeLists.txt;
- build directory is an empty directory, the process of building (construction process will produce some profiles, in order to prevent contamination source, constructed in a separate folder);
The figure CMakeLists.txt document explains:
project(hello)
Configuring project called hello;add_executable(hello src/hello.cpp)
Configured to generate an executable file, the executable file is namedhello
, a file link is involvedsrc/hello.cpp
;
2. Build Solution
By using cmake command, specify the root directory CMakeLists.txt position to generate project solutions.
Above will generate a Visual Studio solution hello.sln
engineering and hello project configuration hello.vcxproj
.
3, building
Previous simply generate a solution, we want hello.exe
does not get created. Can be opened with a graphical interface hello.vcproj
generation may be generated directly from the command line.
In the build directory execute cmake --build .
:
Three, CMake common keywords
1, project management
(1) include_directory
Configuration header file path, can be configured with multiple paths simultaneously. such as:include_directory(CMAKE_SOURCE_DIR/include1 CMAKE_CURRENT_SOURCE_DIR/../include2)
The above contains two variables CMAKE_SOURCE_DIR
, CMAKE_CURRENT_SOURCE_DIR
:
CMAKE_SOURCE_DIR
: CMakeLists.txt the beginning of the path, such as using thecmake ..
build, referring to the CMakeLists.txt path under the project root directory;CMAKE_CURRENT_SOURCE_DIR
: Current CMakeLists.txt the path (there may be a plurality of CMakeLists.txt);
(2) add_executable、add_library
How the former configured to generate an executable file, which is how to generate the configuration database file. Such as:
add_executable(可执行文件名 源码列表)
add_library(动态链接库名称 SHARED 源码列表)
add_library(静态链接库名称 STATIC 源码列表)
复制代码
In the implementation of the link, usually specify additional libraries. CMake by library_directory
the specified link library path, through the target_link_libraries
designated link library name, equivalent to the Makefile
inside -L
and -l
as:
library_directory(CMAKE_BINARY_DIR/lib) # CMAKE_BINARY_DIR是执行cmake命令的目录
target_link_libraries(二进制文件名 链接库名列表)
复制代码
(3) add_subdirectory、include
For the above helloworld
simple to use, you might think to write Makefile
simpler, but if the entire solution like this, you would write Makefile
it?
A single solution project is too big, CMake using divide and conquer to solve. CMake by using a single configuration file for each module, at the root of the introduction of these profiles.
add_subdirectory
The argument is a module directory. CMake will find in the catalogCMakeLists.txt
file, and perform inside the configuration script;include
The argument is a file name, usually similarxxx.cmake
documents;
2, switch options
By performing CMake cmake命令
when -D
option, a control switch of a compiler option. For example, the following CMakeLists.txt
:
project(hello)
option("ENABLE_FEATURE", "whether enable some feature", OFF) # 定义一个开关,是否开启某一个指定功能,默认关闭
if(ENABLE_FEATURE)
add_executable(hello src/hello.cpp feature.cpp) # 假设特定功能就是多链接一个文件
else
add_executable(hello src/hello.cpp)
endif
复制代码
Use when building cmake .. -DENABLE_FEATURE=ON
open specified function, if there is no -D
option or to ON
change OFF
, then closes the specified function.
option defined options can only be used in CMakeLists.txt , 如果想在源码中识别是否定义了某一个开关呢
? add_definition can perform this function. such as:
if(ENABLE_FEATURE)
add_definition(FEATURE) # 如果ENABLE_FEATURE开启,则定义FEATURE宏
elsec
endif
复制代码
By increasing macro definitions in the source code of the macro is determined to achieve the same switching function.
3, debugging information
(1) message
Print information. Format:message(<mode> "debug info: value of arg: ${arg}")
. message function has at least the following modes (MODE):STATUS
, , , . Which mode stops the build in failure and quit.INFO
WARNING
FATAL_ERROR
FATAL_ERROR
4, other variables CMake
(1) CMAKE_C_FLAGS、CMAKE_CXX_FLAGS
Resolution control C / C ++ compiler options. For example: set(CMAKE_CXX_FLAGS "std-c++11 ${CMAKE_CXX_FLAGS}")
# extension CMAKE_CXX_FLAGS
value, adding support for C ++ 11's.
(2) CMAKE_BUILD_TYPE
Specify the type of building, passing by when building using cmake command. such as:cmake .. -DCMAKE_BUILD_TYPE=Release