Meson Quickstart

A quickstart guide for the c/c++ build tool meson

Meson is a build tool for C and C++ projects (amoung others). It has gained attention as a replacement for cmake with gnome-related projects. It is a lot simpiler to use than other build tools such as automake or cmake and features a good level of integration with package managers.

Installation

To use meson for a c/c++ project you’ll need meson, ninja and a compiler ( probably gcc/g++ ). You can install these tools using your package manager.

The meson website also states you can use pip to install meson. Unless you have a spesific reason not to, it’d probably better to stick with the packaged version from your repository.

Fedora

$ sudo dnf update
$ sudo dnf groupinstall -y "Development Tools" # for compiler
$ sudo dnf install -y meson

The development tools package contains tools usually used for software development such as gcc and related tools. The second line installs meson, which should pull ninja in as a dependency.

Ubuntu

FIXME: write this section

$ sudo apt install -y python3 python3-pip python3-setuptools \
  python3-wheel ninja-build

This is the recommended package list from the meson website for Debian. You probably will already have python3 and pip installed.

Arch/Manjaro

FIXME: write this section

I’m leaving this section for now. If you’re using arch you probably won’t need the instructions anyway… If someone who uses arch wants to write this bit feel free to submit a pull request.

Usage

Using meson is actually a collection of tools, meson itself, a build tool called ninja and some related utilities such as pkgconfig. Creating a build file for meson is outside of the scope of this tutorial. If you’re starting a new project refer to the meson website.

To use the project, clone it using git as usual. It should have a meson.build file in the root of the project. This contains the build instructions. We first need to tell meson to generate the files ninja needs to build the project:

$ meson build

The second argument will be the directory name created (in the above example, build). Check the project’s gitignore to see if there is directory marked for build the build folder (this is usually build or builddir).

The project will be built in the build directory, the same as it would be with tools like cmake. You can invoke ninja from either the project root or in the build directory directly.

ninja -c build

or

cd build
ninja

This should compile the software, generating any build artifacts required (shared libraries, excuables, etc…). The project may also have custom build targets, check the project’s readme for details.

Build dependencies

Meson can use either cmake or pkgconfig to find it’s dependencies. I tend to find that pkgconfig works best. In Fedora, packages that provide pkgconfig files are tagged as such, allowing you an easy way to install them.

Fedora

By telling dnf to install pkgconfig(depname) where depname is the Meson dependency name it will find the correct package name automatically and prompt you to install it. This also works in your package files.

$ sudo dnf install -y "pkgconfig(packagename)"

This should pull down the dependency from the repository.

about 600 Words.