一、介绍
如前所述,CMake 是一个元构建系统,可用于为许多其他构建工具创建构建文件。 这个例子展示了如何让 CMake 使用 ninja 构建工具。
二、文件树
$ tree.├── CMakeLists.txt├── main.cpp
- CMakeLists.txt - CMake 命令
- main.cpp - 一个简单的 “Hello World” cpp文件.
三、解析
3.1 生成器
CMake are responsible for writing the input files (e.g. Makefiles) for the underlying build system. Running cmake --help will show the generators available. For cmake v2.8.12.2 the generators supported on my system include: CMakehttps://cmake.org/cmake/help/v3.0/manual/cmake-generators.7.html[generators]负责为基础构建系统编写输入文件(例如 Makefile)。 运行 cmake --help 将显示可用的生成器。 对于 cmake v2.8.12.2,我的系统支持的生成器包括:
GeneratorsThe following generators are available on this platform:Unix Makefiles = Generates standard UNIX makefiles.Ninja = Generates build.ninja files (experimental).CodeBlocks - Ninja = Generates CodeBlocks project files.CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.KDevelop3 = Generates KDevelop 3 project files.KDevelop3 - Unix Makefiles = Generates KDevelop 3 project files.Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.Sublime Text 2 - Unix Makefiles= Generates Sublime Text 2 project files.Generators
As specified in this post, CMake includes different types of generators such as Command-Line, IDE, and Extra generators.如本文所指定,CMake 包括不同类型的生成器,例如命令行,IDE 和其他生成器。
Command-Line Build Tool Generators命令行编译工具生成器
These generators are for command-line build tools, like Make and Ninja. The chosen tool chain must be configured prior to generating the build system with CMake.这些生成器用于命令行构建工具,例如Make和Ninja。 在使用CMake生成构建系统之前,必须先配置所选的工具链。
The supported generators include:支持的生成器包括:
- Borland Makefiles
- MSYS Makefiles
- MinGW Makefiles
- NMake Makefiles
- NMake Makefiles JOM
- Ninja ninja用的
- Unix Makefiles make用的
- Watcom WMake
IDE Build Tool Generators
These generators are for Integrated Development Environments that include their own compiler. Examples are Visual Studio and Xcode which include a compiler natively.这些生成器用于自己有编译器的 IDE。 示例是 Visual Studio 和 Xcode。
The supported generators include:
- Visual Studio 6
- Visual Studio 7
- Visual Studio 7 .NET 2003
- Visual Studio 8 2005
- Visual Studio 9 2008
- Visual Studio 10 2010
- Visual Studio 11 2012
- Visual Studio 12 2013
- Xcode
Extra Generators
These are generators create a configuration to work with an alternative IDE tool and must be included with either an IDE or Command-Line generator.这些生成器,用于其他IDE工具一起使用的配置,并且必须包含在IDE或命令行生成器中。
The supported generators include:
- CodeBlocks
- CodeLite
- Eclipse CDT4
- KDevelop3
- Kate
- Sublime Text 2
| Note | In this example ninja is installed via the command
sudo apt-get install ninja-build
安装ninja的命令 | | —- | —- | | | |
3.2 Calling a Generator
To call a CMake generator you can use the -G command line switch, for example:使用-G参数来唤醒CMake的生成器
cmake .. -G Ninja
After doing the above CMake will generate the required Ninja build files, which can be run from using the ninja command.完成上述操作后,CMake将生成所需的Ninja构建文件,可以使用ninja命令运行该文件
$ cmake .. -G Ninja$ lsbuild.ninja CMakeCache.txt CMakeFiles cmake_install.cmake rules.ninja
四、构建示例
Below is sample output from building this example.
$ mkdir build.ninja$ cd build.ninja/$ cmake .. -G Ninja-- The C compiler identification is GNU 4.8.4-- The CXX compiler identification is GNU 4.8.4-- Check for working C compiler using: Ninja-- Check for working C compiler using: Ninja -- works-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Check for working CXX compiler using: Ninja-- Check for working CXX compiler using: Ninja -- works-- Detecting CXX compiler ABI info-- Detecting CXX compiler ABI info - done-- Configuring done-- Generating done-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/J-building-with-ninja/build.ninja$ ninja -v[1/2] /usr/bin/c++ -MMD -MT CMakeFiles/hello_cmake.dir/main.cpp.o -MF "CMakeFiles/hello_cmake.dir/main.cpp.o.d" -o CMakeFiles/hello_cmake.dir/main.cpp.o -c ../main.cpp[2/2] : && /usr/bin/c++ CMakeFiles/hello_cmake.dir/main.cpp.o -o hello_cmake -rdynamic && :$ lsbuild.ninja CMakeCache.txt CMakeFiles cmake_install.cmake hello_cmake rules.ninja$ ./hello_cmakeHello CMake!
