2.切换生成器

2.切换生成器

显示构建器 (cmake –help)

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$ cmake --help
...
Generators

The following generators are available on this platform (* marks default):
* Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
Use -A option to specify architecture.
Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
Optional [arch] can be "Win64" or "IA64".
Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
Optional [arch] can be "Win64" or "IA64".
Borland Makefiles = Generates Borland makefiles.
NMake Makefiles = Generates NMake makefiles.
NMake Makefiles JOM = Generates JOM makefiles.
MSYS Makefiles = Generates MSYS makefiles.
MinGW Makefiles = Generates a make file for use with
mingw32-make.
Green Hills MULTI = Generates Green Hills MULTI files
(experimental, work-in-progress).
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
Ninja Multi-Config = Generates build-<Config>.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles JOM
= Generates CodeBlocks project files.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - MinGW Makefiles = Generates CodeLite project files.
CodeLite - NMake Makefiles = Generates CodeLite project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Eclipse CDT4 - NMake Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - MinGW Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
Kate - MinGW Makefiles = Generates Kate project files.
Kate - NMake Makefiles = Generates Kate project files.
Kate - Ninja = Generates Kate project files.
Kate - Unix Makefiles = Generates Kate project files.
Sublime Text 2 - MinGW Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 - NMake Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.

具体实施(用 -G 切换生成器)

  1. 首先,使用以下步骤配置项目:

    1
    2
    3
    $ mkdir build
    $ cd build
    $ cmake -G "CodeBlocks - Unix Makefiles" ..

如何工作

与前一个配置相比,每一步的输出没什么变化。每个生成器都有自己的文件集,所以编译步骤的输出和
构建目录的内容是不同的:
build.ninja 和 rules.ninja :包含 Ninja 的所有的构建语句和构建规则。
CMakeCache.txt :CMake 会在这个文件中进行缓存,与生成器无关。
CMakeFiles :包含由 CMake 在配置期间生成的临时文件。
cmake_install.cmake :CMake 脚本处理安装规则,并在安装时使用。
cmake –build . 将 ninja 命令封装在一个跨平台的接口中。

测试代码

C示例:

1
2
3
4
5
6
7
8
9
10
// hello-world.c
#include <stdio.h>
#include <stdlib.h>

char *say_hello() { return "Hello, CMake world!"; }

int main() {
printf("%s\n", say_hello());
return EXIT_SUCCESS;
}
1
2
3
4
5
6
7
8
# CMakeLists.txt
# 设置最低 cmake 版本
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# 项目名称和语言
project(recipe-01 LANGUAGES C)

add_executable(hello-world hello-world.c)

C++示例:

1
2
3
4
5
6
7
8
9
10
11
//hello-world.cpp
#include <cstdlib>
#include <iostream>
#include <string>

std::string say_hello() { return std::string("Hello, CMake world!"); }

int main() {
std::cout << say_hello() << std::endl;
return EXIT_SUCCESS;
}
1
2
3
4
5
6
7
8
# CMakeLists.txt
# 设置最低 cmake 版本
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# 项目名称和语言
project(recipe-01 LANGUAGES CXX)

add_executable(hello-world hello-world.cpp)