Skip to content

Instantly share code, notes, and snippets.

@cynthia2006
Created May 10, 2025 14:55
Show Gist options
  • Select an option

  • Save cynthia2006/d30e1b3f504aef0cb3f16bbdbd5e562e to your computer and use it in GitHub Desktop.

Select an option

Save cynthia2006/d30e1b3f504aef0cb3f16bbdbd5e562e to your computer and use it in GitHub Desktop.

Notes on Meson

Meson is primarily a C/C++ build-system, arguably better than CMake or GNU Autotools in terms of UX.

Preliminary Points

These points are essential to both the programmer and the end user who builds the project.

  • A file meson.build must exist at the top-level directory of the project.

  • Meson doesn't allow “in-source builds”, meaning it requires a separate build directory. As customary, separate directories are to made for separate configurations—debug or release.

    $ meson setup builddir
    $ meson compile -C builddir
  • Configuration step only needs to be performed once, the build-system thereafter automatically detects any changes in source code or DSL during the Build step.

  • Meson uses the Ninja backend by default, which can be changed through --backend option supplied in configuration step. But, using Ninja is a more feasible option than generating Makefiles which none has any idea of.

  • Meson has major four build types, namely—plain (no compiler options), debug, debugoptimized (usually -O2 -g), release; all can be specified through --buildtype option during configuration step.

    $ meson setup builddir --buildtype=release
  • Meson understands environment variables like CC and CXX, which could be used to control the choice of compilers during configuration step, e.g. using clang instead of gcc.

    $ CC=clang meson setup builddir
  • Meson will only install build targets explicitly tagged (see here) as installable. The default installation root is /usr/local, which can be changed through --prefix.

    $ meson install -C builddir --prefix ~/.local

Basics

A Meson project is defined in meson.build (a Python-like DSL); the directory in which it exists becomes the root of the project. A project is first declared with the project() directive containing—name, version, languages used within the project. Each project is equipped with a set of build targets of types either—static, shared or executable; defined by static_library(), shared_library(), executable() directives respectively. Each build target generates build artifacts in the build directory.

# The bare minimum of a Meson project, consisting of a single source file `hello.c'
# whose name suggests that it prints `Hello, world!' on screen. When the project is
# built, it generates a build artifact named `hello' in the builddir.
project('hello-world', 'c', version : '1.0.0')
executable('hello', 'hello.c')

To build and run the following steps are taken.

$ meson setup builddir
$ meson compile -C builddir
$ ./builddir/hello
Hello, world!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment