Article Directory
1. What is Meson?
Meson is a compilation system designed to build quickly, and can be built on multiple platforms, supporting Linux, Windows and MacOS. Not only supports compiling C/C++, but also supports D, Fortran, Java, Rust and other languages.
Meson is implemented based on Python3 and depends on Ninja. Meson and Ninja complement each other. Meson takes care of building the project dependencies and Ninja does the compilation. Ninja is also a lightweight compilation system, and I will introduce Ninja later when I find an opportunity.
The official website of Meson is: https://mesonbuild.com
2. Install
-
install
Python3
andpip3
-
Install
Ninja
apt way:
$ sudo apt-get install ninja-build
pip3 way:
$ pip3 install ninja
-
Install
Meson
$ pip3 install meson
It can also be installed through the apt method, only due to the relationship between the source, the installed version may be relatively low, so it is recommended to use the
pip3
installation method.
3. Hello World
In this section, we use Meson to build a simple C language program.
-
Create a C language hello world program:
#include <stdio.h> int main(int argc, char **argv) { printf("Hello there.\n"); return 0; }
-
Create a compilation script in the same directory
meson.build
:project('tutorial', 'c') executable('demo', 'main.c')
-
Set the compilation directory (builddir is the directory name, and all files generated by compilation will be automatically stored in this directory):
$ meson setup builddir
After execution, the system will print out the configuration information of the project:
The Meson build system Version: 1.0.0 Source dir: /home/xupeng/work/project/tmp/meson Build dir: /home/xupeng/work/project/tmp/meson/builddir Build type: native build Project name: tutorial Project version: undefined C compiler for the host machine: ccache cc (gcc 5.4.0 "cc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609") C linker for the host machine: cc ld.bfd 2.26.1 Host machine cpu family: x86_64 Host machine cpu: x86_64 Build targets in project: 1
-
Enter the compilation directory to compile the code:
$ cd builddir $ ninja # 或者 $ cd builddir $ meson compile
We can also compile directly in the original directory:
$ meson compile -C builddir
After execution, the system will print the compilation information:
INFO: autodetecting backend as ninja INFO: calculating backend command to run: /usr/bin/ninja [2/2] Linking target demo
-
After the compilation is successful, the target executable file demo is generated in the compilation directory:
$ ./demo Hello there.
4. Compile the library
Earlier we used Meson to compile an executable file. If we need to compile an so library, we only need to meson.build
adjust it to:
project('tutorial', 'c')
# 将编译可执行文件的方式改成编译库的方式
# executable('demo', 'main.c')
library('demo', 'main.c')
Execute again meson compile
, and the target library will be generated in the compilation directory libdemo.so
.
5. Meson script
Meson's compilation configuration and scripts are written in meson.build
, so if you want to perform various compilation configurations, you only need to complete the script. Meson provides a series of syntax and API for us to make more flexible and powerful configuration.
In terms of syntax, because Meson is implemented based on Python3, the syntax of Meson scripts is very similar to Python. For details, please refer to the syntax section of the official website - https://mesonbuild.com/Syntax.html
Other compilation configurations and scripts mainly include:
- Compilation Type - Configure whether to compile as an executable or a library
- header file - path to the header file that configuration depends on
- Install - configure the installation actions
meson install
after - Parameters - configuration input parameters at compile time
- Dependency - Dependency library when configuring compile time
- Threads - configures threads at compile time
- externalcommand - Executes an external shell command
- Generate - Code Preconfiguration
- test - compiles and executes unit tests
- Cross-compilation - cross-compilation information configuration
- Compile Options -
meson_options.txt
Dynamic configuration of compiled modules in - Subproject - configuration of subproject
As you can see, there are still a lot of configurable items. For details, please refer to the manual section of the official website - https://mesonbuild.com/Manual.html .