# build-system **Repository Path**: chendilin/build-system ## Basic Information - **Project Name**: build-system - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2023-02-12 - **Last Updated**: 2023-02-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 常见构建系统学习 构建系统(build system)是用来从源代码生成用户可以使用的目标(targets)的自动化工具,目标可以包括库、可执行文件、或者生成的脚本等等。 当前主流的编译基础工具是makefile,当时makefile有缺点: 1. 大型工程手写makefile费事费力 2. makefile隐式规则影响构建效率 所以,又出现了很多新的构建工具来解决这两个问题,如 [CMake](www.cmake.org/), [XMake](https://xmake.io/), [qmake](https://doc.qt.io/qt-5/qmake-manual.html), [Scons](https://www.scons.org/), [Ninja](https://ninja-build.org/), [Meson](https://mesonbuild.com/), Bazel等等。 这些构建工具的使用,我们举例并使用下。 假设我们的目录如下,main.c需要使用hello.c中的函数`hello()`: ```shell app/ ├── hello │ ├── hello.c │ └── hello.h └── main.c ``` 其中,hello.c内容为 ```c #include int hello(void) { printf("hello, world!\n"); return 0; } ``` hello.h内容为 ```c #ifndef HELLO_H_ #define HELLO_H_ int hello(void); #endif ``` main.c内容为 ```c #include "hello.h" int main(void) { hello(); return 0; } ``` ## Shell 如果不用任何构建工具,直接使用最原始的构建脚本,内容如下,执行如下脚本,会在当前目录生成可执行程序`app` ```shell #!/bin/bash gcc -c main.c -I hello gcc -c hello/hello.c gcc main.o hello.o -o app ``` ## Makefile 一个工程中的源文件不计其数,其按类型、功能、模块分别放在若干个目录中,makefile定义了一系列的规则来指定哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为 makefile就像一个Shell脚本一样,也可以执行操作系统的命令。 上述工程的makefile文件如下,使用`make`命令执行,会在当前目录生成可执行程序`app` ```makefile app: main.o hello.o gcc main.o hello.o -o app main.o: main.c gcc -c main.c -I hello hello.o: hello/hello.c gcc -c hello/hello.c clean: rm -rf main.o hello.o app ``` 或者进阶版 ```makefile TARGET = app SRCS = main.c hello/hello.c INC = -Ihello OJBS = $(SRCS:.c=.o) $(TARGET): $(OJBS) echo $(OJBS) $(CC) -o $@ $^ %.o: %.c $(CC) $(INC) -o $@ -c $< clean: rm -rf $(TARGET) $(OJBS) ``` ## CMake CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。 CMake 的组态档取名为 CMakeLists.txt,Cmake 并不直接建构出最终的软件,而是产生标准的建构档(如 Unix 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然后再依一般的建构方式使用。 如下为当前目录下CMakeLists.txt文件内容 ```cmake cmake_minimum_required(VERSION 2.8) project(app) set(hello hello/hello.c hello/hello.h) include_directories(hello) add_executable(app main.c ${hello}) ``` 执行命令 ```shell mkdir build && cd build cmake .. # 在build目录下生成makefile make # 在build目录生成可执行程序app ``` 以上是老版本CMake的写法,CMake 3.xx版本增加了模块化target的写法,推荐使用。 ## XMake XMake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/cmake,配置语法更加简洁直观,对新手非常友好,短时间内就能快速入门,能够让用户把更多的精力集中在实际的项目开发上。 xmake.lua可通过`xmake`命令生成,更加灵活的配置修要手动修改,内容如下 ```lua target("app") set_kind("binary") add_files("main.c") add_files("hello/hello.c") add_includedirs("hello") ``` 有了xmake.lua后,执行命令 ```shell xmake # 编译 xmake run app # 运行 xmake run -d app # gdb调试 ``` ## Scons Scons是一个开放源码、以Python语言编码的自动化构建工具,可用来替代make编写复杂的makefile。并且scons是跨平台的,只要 Scons脚本写的好,可以在Linux和Windows下随意编译。 SCons 的设计目标就是让开发人员更容易、更可靠和更快速的建造软件。 上述工程,需要在工程目录创建一个`SConstruct`文件,内容如下 ```python Program( target = "app", source = ["main.c", "hello/hello.c"], CPPPATH = ["hello"] ) ``` 然后在命令行执行 ```shell scons # 编译 scons -c # 清除编译结果 ``` ## Ninja Ninja 是Google的一名程序员推出的注重速度的构建工具,一般在Unix/Linux上的程序通过make/makefile来构建编译,而Ninja通过将编译任务并行组织,大大提高了构建速度,Ninja的定位非常清晰,就是达到更快的构建速度。 如上工程,ninja编译需要`build.ninja`文件。如果有CMakeLists.txt,可通过 `cmake -G Ninja` 生成,没有的话,可以手写一个,内容如下 ```shell ninja_required_version = 1.5 # 编译 rule cc command = gcc -c -Ihello $in -o $out description = compiling $in to $out ... build main.o: cc main.c build hello.o: cc hello/hello.c # 链接 rule link command = gcc $in -o $out description = linking $in to $out ... build app: link main.o hello.o ``` 然后在命令行执行 ```shell ninja # 编译 ninja -t clean # 清除编译结果 ``` ## Meson Meson(The Meson Build System)是个项目构建系统,如Makefile,automake,CMake等等。Meson是一个Python实现的开源项目,编译需要Ninja命令, 其思想是,开发人员花费在构建调试上的每一秒都是浪费,同样等待构建过程直到真正开始编译都是不值得的。 Meson需要meson.build文件,手动编辑,内容如下 ```python project('app', 'c') src = ['main.c', 'hello/hello.c'] inc = 'hello' executable('app', src, include_directories: inc) ``` 然后在命令行执行 ```shell meson _build # 生成_build(这里加下划线是为了与cmake的build区分下)目录,下面有build.ninja ninja -C _build # 编译 ``` 当前很多开源软件构建工具已经转meson了,以前的开源软件按照老套路`./configure && make && sudo make install` 要换成`meson build && ninja -C build && sudo ninja -C build install`了。 ## 其他 当然,还有[Bazel](https://bazel.build/),[Buck](https://buck.build/),[qmake](https://doc.qt.io/qt-5/qmake-manual.html),[Autotools](http://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html)等构建系统,就不一一示范了。