一、介绍

如前所述,CMake 是一个元构建系统,可用于为许多其他构建工具创建构建文件。 这个例子展示了如何让 CMake 使用 ninja 构建工具。

二、文件树

  1. $ tree
  2. .
  3. ├── CMakeLists.txt
  4. ├── main.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,我的系统支持的生成器包括:

  1. Generators
  2. The following generators are available on this platform:
  3. Unix Makefiles = Generates standard UNIX makefiles.
  4. Ninja = Generates build.ninja files (experimental).
  5. CodeBlocks - Ninja = Generates CodeBlocks project files.
  6. CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  7. Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
  8. Eclipse CDT4 - Unix Makefiles
  9. = Generates Eclipse CDT 4.0 project files.
  10. KDevelop3 = Generates KDevelop 3 project files.
  11. KDevelop3 - Unix Makefiles = Generates KDevelop 3 project files.
  12. Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
  13. Sublime Text 2 - Unix Makefiles
  14. = 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的生成器

  1. 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命令运行该文件

  1. $ cmake .. -G Ninja
  2. $ ls
  3. build.ninja CMakeCache.txt CMakeFiles cmake_install.cmake rules.ninja

四、构建示例

Below is sample output from building this example.

  1. $ mkdir build.ninja
  2. $ cd build.ninja/
  3. $ cmake .. -G Ninja
  4. -- The C compiler identification is GNU 4.8.4
  5. -- The CXX compiler identification is GNU 4.8.4
  6. -- Check for working C compiler using: Ninja
  7. -- Check for working C compiler using: Ninja -- works
  8. -- Detecting C compiler ABI info
  9. -- Detecting C compiler ABI info - done
  10. -- Check for working CXX compiler using: Ninja
  11. -- Check for working CXX compiler using: Ninja -- works
  12. -- Detecting CXX compiler ABI info
  13. -- Detecting CXX compiler ABI info - done
  14. -- Configuring done
  15. -- Generating done
  16. -- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/J-building-with-ninja/build.ninja
  17. $ ninja -v
  18. [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
  19. [2/2] : && /usr/bin/c++ CMakeFiles/hello_cmake.dir/main.cpp.o -o hello_cmake -rdynamic && :
  20. $ ls
  21. build.ninja CMakeCache.txt CMakeFiles cmake_install.cmake hello_cmake rules.ninja
  22. $ ./hello_cmake
  23. Hello CMake!