TExcelTAD
Home › Meson, Autotools & xmake › Meson: First Project

Meson: First Project

Meson is a build system generator, like CMake: you describe the project in meson.build, Meson configures it once into a build directory and writes a build.ninja, and Ninja does the building. It was started in 2013 by Jussi Pakkanen with two goals: builds should be fast, and the build description should be easy to read and hard to get wrong. It is written in Python 3, installs with pip install meson (the course image uses Meson 1.12.1 from PyPI), and needs Ninja at build time.

The four commands

Step Command CMake equivalent
Configure meson setup build cmake -S . -B build -G Ninja
Build meson compile -C build (or just ninja -C build) cmake --build build
Test meson test -C build ctest --test-dir build
Install meson install -C build cmake --install build

There is no "source directory" option: meson setup build is run from the source root and creates build/ there; meson setup builddir srcdir also works. -C build on the other commands means "the build directory is build", like make -C.

A first project

bash · try it yourself
mkdir -p hello && cd hello
cat > meson.build <<'M'
project('hello', 'c',
  version: '1.0.0',
  default_options: ['warning_level=3', 'c_std=c17'])

executable('hello', 'main.c')
M
cat > main.c <<'SRC'
#include <stdio.h>
int main(void) { puts("hello from meson"); return 0; }
SRC
meson setup build
meson compile -C build -j 1
./build/hello
The Meson build system
Version: 1.12.1
Source dir: /home/learner/hello
Build dir: /home/learner/hello/build
Build type: native build
Project name: hello
Project version: 1.0.0
C compiler for the host machine: /usr/local/bin/ccache cc (gcc 13.3.0 "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0")
C linker for the host machine: cc ld.bfd 2.42
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1

Found ninja-1.13.2 at /usr/local/bin/ninja
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /usr/local/bin/ninja -C /home/learner/hello/build -j 1
ninja: Entering directory `/home/learner/hello/build'
[1/2] Compiling C object hello.p/main.c.o
[2/2] Linking target hello
hello from meson

Read the configure output from top to bottom: Meson found the C compiler (and noticed ccache on the PATH, so it wraps the compiler with it automatically), the linker, the machine it builds for, and Ninja. Then meson compile ran ninja -C build -j 1.

The project() call is always first. Its arguments:

  • the project name, then the languages ('c', 'cpp', or a list such as ['c', 'cpp']; Meson also knows Rust, Fortran, Objective-C, D, Vala, Cython, CUDA…);
  • version:, used by meson dist, pkg-config files and meson.project_version();
  • default_options:, defaults for built-in options (warning level, language standard, build type, library type) that the user can still override with -D on the command line.

Rebuilding

The build directory knows how to update itself. Change a source file and only that file is recompiled; change meson.build and Meson re-runs its configure step automatically before building:

bash · try it yourself
mkdir -p hello && cd hello
cat > meson.build <<'M'
project('hello', 'c')
executable('hello', 'main.c')
M
echo 'int main(void) { return 0; }' > main.c
meson setup build > /dev/null
meson compile -C build -j 1 > /dev/null
echo "--- nothing changed"
meson compile -C build -j 1 | tail -1
echo "--- main.c changed"
sleep 1 && echo 'int main(void) { return 1; }' > main.c
meson compile -C build -j 1 | grep '^\['
echo "--- meson.build changed"
sleep 1 && echo "message('the build file changed')" >> meson.build
meson compile -C build -j 1 | grep -E 'Message|Regenerat|^\['
--- nothing changed
ninja: no work to do.
--- main.c changed
[1/2] Compiling C object hello.p/main.c.o
[2/2] Linking target hello
--- meson.build changed
[0/1] Regenerating build files
Message: the build file changed

A build directory per configuration

Because all state lives in the build directory, you keep one directory per configuration, side by side, and never mix them: debug and release, GCC and Clang, native and cross. Configuration options are passed with -D or as dedicated flags such as --buildtype:

bash · try it yourself
mkdir -p hello && cd hello
cat > meson.build <<'M'
project('hello', 'c')
executable('hello', 'main.c')
M
echo 'int main(void) { return 0; }' > main.c
meson setup build-debug > /dev/null
meson setup build-release --buildtype=release > /dev/null
CC=clang meson setup build-clang > /dev/null
for d in build-debug build-release build-clang; do
  printf '%-14s ' $d
  meson introspect $d --buildoptions > opts.json
  python3 -c '
import json
opts = {o["name"]: o["value"] for o in json.load(open("opts.json"))}
print("buildtype=%s optimization=%s debug=%s" % (opts["buildtype"], opts["optimization"], opts["debug"]))'
done
grep -h "C compiler for the host" build-*/meson-logs/meson-log.txt | sort | uniq | cut -c1-70
build-debug    buildtype=debug optimization=0 debug=True
build-release  buildtype=release optimization=3 debug=False
build-clang    buildtype=debug optimization=0 debug=True
C compiler for the host machine: /usr/local/bin/ccache cc (gcc 13.3.0
C compiler for the host machine: clang (clang 18.1.3 "Ubuntu clang ver

The compiler is picked once, at meson setup time, from CC/CXX (or a native file), and stored in that build directory. The build directory lesson explains why you cannot change it later, and meson introspect is the tool used above to read the options back.

Tip: Meson's default build type is debug (-O0 -g), not "no flags" as in CMake. For benchmarking or packaging use --buildtype=release or debugoptimized.

Test yourself

What does meson compile -C build do?

  1. Configures the project into build/
  2. Runs Ninja in build/ (re-running Meson first if a meson.build changed)
  3. Compiles every source file without Ninja
  4. Deletes and recreates build/

Where do you declare a project's languages in Meson?

  1. In meson.options
  2. As the arguments after the name in project()
  3. With --lang at meson setup
  4. They are detected from file extensions only

You want a GCC and a Clang build of the same source tree. What is the Meson way?

  1. Edit meson.build to switch the compiler
  2. Two build directories, e.g. meson setup build-gcc and CC=clang meson setup build-clang
  3. Run meson configure build -Dc=clang
  4. Delete build/ each time you switch