Basic 10-building-with-ninjia
# 介绍
CMake 是一个 meta-build system,它支持生成很多构建工具对应的构建文件。这个例子展示如何使用 CMake 产生 nijia build (opens new window) 的构建文件,然后使用 nijia 进行项目构建。
你可以理解为 CMake 是一个高级的构建工具,它能构建出低级的构建工具所需要的构建文件。然后使用低级的构建工具对程序进行编译和汇编。
本例的目录🌲:
.
├── CMakeLists.txt
├── main.cpp
1
2
3
2
3
CMakeLists.txt:
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (hello_cmake)
# Add an executable
add_executable(hello_cmake main.cpp)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 概念解释
CMake 生成器 (Generator)
CMake 生成器是通过读取 CMakeLists.txt 文件生成低级构建工具的构建文件的工具。可以使用 cmake --help 命令查看 CMake 生成器可以生成哪些构建工具的构建文件:
$ cmake --help
...
...
Generators
The following generators are available on this platform (* marks default):
* Unix Makefiles = Generates standard UNIX makefiles.
Green Hills MULTI = Generates Green Hills MULTI files
(experimental, work-in-progress).
Ninja = Generates build.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.
Kate - Ninja = Generates Kate project files.
Kate - Unix Makefiles = Generates Kate project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
可以看到 CMake 在我的 ubuntu 20.04 上支持生成以上构建工具的构建文件。
# Calling a Generator
为了调用 CMake 的 generator 可以在 CMake 构建时使用:
cmake .. -G Ninja
1
运行完上面命令后就可以直接使用 Ninja 进行构建了。
# 构建本例
$ 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 && :
$ ls
build.ninja CMakeCache.txt CMakeFiles cmake_install.cmake hello_cmake rules.ninja
$ ./hello_cmake
Hello CMake!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
上次更新: 12/27/2023, 8:55:47 AM