Compile Vala With Meson

In my previous post Getting Started With Vala I showed you how to write a program in Vala and compile it using valac. Compiling with valac was okay but even with only GTK+ as our dependency the command is starting to get long. If you don’t want to specify all the compiler flags each time you can use a build system.

For a long time GNOME developers used the Autotools build systems for most of their applications but recently there have been a migration to Meson so in this post I will show you how to compile a simple Vala program with Meson.

So this is how a simple meson.build for Vala looks like:

project('example', 'vala', 'c')

dependencies = [
	dependency('glib-2.0'),
	dependency('gobject-2.0')
]

executable('valaprog', 'main.vala',
	   dependencies : dependencies)

It might look more complicated than a simple command but as your project will start to grow you will see how useful it is to have a build system. Before using Meson let’s look and understand what this file does.

First we use the project function to tell meson about are project and what languages we use. As you can see we also wrote c, that’s because Vala compile to C before compiling to an executable file.

After that we are declaring an array of dependencies that our project needs. Every Vala program implicitly require GLib and GObject so we need to add them even though we don’t use them in our code.

Now that we have declared our project and dependencies, in order to actually create our executable file we need to call the executable function with our desired output name, entry file and dependencies as parameters. The reason we wrote dependencies : dependencies is because the first dependencies is the name of the parameter.

Now that we know what our meson.build file does, let’s tell Meson to use our build file to compile our program. Assuming you have Meson installed, all you need to do is to run this command in your project directory with the destination directory as an argument:

meson build

Meson doesn’t actually compile our programs, instead it generate build files according to the operating system we are using. On Linux it will create ninja files, so let’s cd into the directory and run ninja. Now you should have an executable file called valaprog in the build directory.