Meson Tutorial for Beginners Pt. 1
Meson is a user friendly build system. It uses the Ninja build system under the hood. We need to install a couple of software.
- Meson: The subject of this tutorial. For Debian/Ubuntu type
sudo apt install mesonand it will install Meson for you. - Ninja: When you install Meson, it will also install Ninja for you.
- GCC: A C compiler. You can install via
sudo apt install gccor as part of build essential assudo apt install build-essential. The tutorial uses the C language as an example. That is why we need GCC.
Go ahead and create a folder with the name hello_meson. Inside this folder, create two files - main.c and meson.build.
mkdir hello_meson
cd hello_meson
touch main.c
touch meson.buildOpen the main.c file and write the following C code.
#include <stdio.h>
int main(void) {
printf("hello from meson\n");
return 0;
}Open the meson.build file and write the following code.
project('hello-meson', 'c')
executable('hello', 'main.c')The meson.build file is the manifest file for the Meson build system.
This first line must be the project() function. The first argument to the function is, name of the project. It is different from the executable of the program. It is like the distribution name of the project. Consider that you want to distribute this project as tarball. What name should you give to that tarball? That’s the project name as the first argument of project() function. The second argument is the language of the project which is c in our case. Don’t forget that you need to use single quotes and not double quotes.
Next we used the executable() function. The first argument is the name of the output or the executable e.g. hello and the second argument is the name of the file used to compile the output e.g. main.c.
We are now ready to run the project.
Open the terminal, go inside the hello_meson folder and run the following command.
meson setup builddirThis command sets up the project and creates a builddir folder with necessary files and folders needed for the Ninja build system. You need to run this command only once at the start of the project (think of it as npm init in Node). You should see some output that verifies that set up is ready to compile your project.
Next, run the following command to compile the project.
meson compile -C builddirThis command compiles the project and places the compiled output within builddir. Now, we can run the executable as,
./builddir/hello
hello from mesonIf we change the name of the executable in meson.build to main from hello, Meson will generate main as an executable after successful compilation.
project('hello-meson', 'c')
executable('main', 'main.c')As you can notice from the above commands, we’re not running the meson setup builddir command. As I mentioned previously, you don’t have to run it again.
Let’s now print “hello, world” text instead of “hello from meson”.
#include <stdio.h>
int main(void) {
printf("hello world\n");
return 0;
}You know the commands to see print the “hello, world” text, right?
meson compile -C builddir
./builddir/main
hello worldYay!