build system(构建系统)
构建系统是一个广义的术语,它将用于编译和链接源代码的一系列工具组合在一起,但也包括在构建过程中使用的辅助工具。
常用的build system:ninja,make,MSBuild等.
问题
假设你有两个源文件source file1和source file2,两个lib,两个app,相互之间的引用关系如上图所示.所以有四个编译目标,两个lib,两个app,编译时需要处理:
-
多文件相互引用
一个源文件会被多个源文件所引用,文件之间的引用关系会会错综复杂,有可能出现A→B→C→D的关系,编译目标经过层层引用可能难以确认.
-
多个编译目标
整个项目会有多个编译目标,很多的库和应用程序,例如上图中的lib1&2,app
-
编译目标的先后顺序
有一些编译编译目标是有依赖关系的,只有在先编译了A,才能编译B.例如上图中的lib1和app1.
手写GCC命令编译
-
引用文件容易出错
-
编译目标难以确认
一个源文件会被多个源文件所引用,文件之间的引用关系会会错综复杂,有可能出现A→B→C→D的关系,编译目标经过层层引用可能难以确认.
-
先后顺序混乱
多个编译目标,不能确认之间的依赖顺序,导致无法确认编译顺序.
-
耗时长
这个是最显而易见的的,你可能需要为编译准备工作花很长时间.
make解决方案
编写makefile,确认文件之间的依赖关系,确认编译目标需要引用的文件.make工具会解析makefile,确定本次需要的编译目标,编译的先后顺序,让gcc自动完成编译任务.
优势:
- 自动确认编译目标
- 自动确认编译的先后顺序
新的问题
make是平台相关的,不同平台的make工具链和makefile都有所不同.
多版本make:
- GNU make:是GNU操作系统中的make工具.
- Nmake:windows出品的在windows平台上使用的工具
- make:unix标准make
- Qmake:qt自带的make
如果是一个项目在多个平台上都需要部署的话,那么可能你需要使用多个平台的make工具,编写各自的makefile.有没有一种跨平台的解决方案?
CMake解决方案
Cmake是一种跨平台的build system,或者叫meta build system,他是一种更高纬度,更抽象的build system,它可以生成不同版本的makefile,然后让对应的make工具去执行,从而完成编译问题.
Cmake用户只需要编辑CMakeLists.txt,选择自己的genertator,就能完成配置文件.
例如一台电脑上可能安装了多个build system,比如 Windows 系统可能安装了 Visual Studio 和 MinGW , CMake 允许你指定为哪个构建系统生成配置文件。
优势:
- 跨平台
- 解决复杂的依赖管理
- 重用和共享
- 集成测试和安装
- 项目维护和团队协作
CMake流程图
- 程序员编译自己的CMakeList.txt
- CMake会根据CMakeList.txt,使用Generator,生成对应的makefile或者是其他build system需要的配置文件.
- build system获取到Generator的输出的配置文件后,制定自己的编译和链接任务.
- 最终生成库文件或者是可执行文件.
Genertator
CMake 并不编译或链接任何源文件,它使用Genertator为build system创建配置文件吗,build system使用这些文件来编译和链接源代码文件。
generator是存在多种类型的,它于系统以及build system存在对象关系,CMake会根据你当前环境自动选择,用户也可以手动指定具体的generator.
CMake支持的Generators
CMake支持的Generators(命令行执行cmake —help)
Generators
The following generators are available on this platform (* marks default):
Visual Studio 17 2022 = Generates Visual Studio 2022 project files.
Use -A option to specify architecture.
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] = Deprecated. Generates Visual Studio 2013
project files. Optional [arch] can be
"Win64" or "ARM".
Visual Studio 9 2008 [arch] = Deprecated. Generates Visual Studio 2008
project files. Optional [arch] can be
"Win64" or "IA64".
Borland Makefiles = Generates Borland makefiles.
* NMake Makefiles = Generates NMake makefiles. //当前在windows系统,所以系统自动设定位NMake
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
(deprecated).
CodeBlocks - NMake Makefiles = Generates CodeBlocks project files
(deprecated).
CodeBlocks - NMake Makefiles JOM
= Generates CodeBlocks project files
(deprecated).
CodeBlocks - Ninja = Generates CodeBlocks project files
(deprecated).
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files
(deprecated).
CodeLite - MinGW Makefiles = Generates CodeLite project files
(deprecated).
CodeLite - NMake Makefiles = Generates CodeLite project files
(deprecated).
CodeLite - Ninja = Generates CodeLite project files
(deprecated).
CodeLite - Unix Makefiles = Generates CodeLite project files
(deprecated).
Eclipse CDT4 - NMake Makefiles
= Generates Eclipse CDT 4.0 project files
(deprecated).
Eclipse CDT4 - MinGW Makefiles
= Generates Eclipse CDT 4.0 project files
(deprecated).
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files
(deprecated).
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files
(deprecated).
Kate - MinGW Makefiles = Generates Kate project files (deprecated).
Kate - NMake Makefiles = Generates Kate project files (deprecated).
Kate - Ninja = Generates Kate project files (deprecated).
Kate - Ninja Multi-Config = Generates Kate project files (deprecated).
Kate - Unix Makefiles = Generates Kate project files (deprecated).
Sublime Text 2 - MinGW Makefiles
= Generates Sublime Text 2 project files
(deprecated).
Sublime Text 2 - NMake Makefiles
= Generates Sublime Text 2 project files
(deprecated).
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files
(deprecated).
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files
(deprecated).
vscode中的generator
[main] 正在配置项目: Cmake workspace
[proc] 执行命令: "D:\Program Files (x86)\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE "-DCMAKE_C_COMPILER:FILEPATH=D:\Program Files (x86)\msys64\mingw64\bin\gcc.exe"
"-DCMAKE_CXX_COMPILER:FILEPATH=D:\Program Files (x86)\msys64\mingw64\bin\g++.exe" "-SD:/c++ workspace/Cmake workspace" "-Bd:/c++ workspace/Cmake workspace/build" **-G "MinGW Makefiles"**
[cmake] Not searching for unused variables given on the command line.
[cmake] CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
[cmake] Compatibility with CMake < 3.5 will be removed from a future version of
[cmake] CMake.
[cmake]
[cmake] Update the VERSION argument <min> value or use a ...<max> suffix to tell
[cmake] CMake that the project does not need compatibility with older versions.
[cmake]
[cmake]
[cmake] -- Configuring done (0.3s)
[cmake] -- Generating done (0.3s)
[cmake] -- Build files have been written to: D:/c++ workspace/Cmake workspace/build
从输出框图中可以看到当前项目使用的是”MinGW Makefiles”.