I am new to Vala, learning to write GTK apps for elementary OS Hera (based on Ubuntu 18.04.3). I couldn't find any documentation on how to go about writing a make file for my programs.
I wish to organise my .vala files under the 'src' folder in the root of project. I also have no idea on how to specify the name of executable to the vala compiler. It just takes the name from the name of the vala source file.
Could someone specify a syntax for the makefile in Vala?
The Vala compiler, valac
, has the --output
argument to name the generated binary file. For example:
valac my_source_file.vala --output myprogram
Use valac --help
to find more compiler options.
A simple Makefile
could be:
sources = $(wildcard src/*.vala)
myprogram:
valac $(sources) --output myprogram
Save this as Makefile
in the project directory and issue the command make
or make myprogram
and you will have the binary file myprogram
built. There are a few things to note:
src
directoryMany new Vala projects are using the Meson build system because it is very fast and has a cleaner syntax. With Meson a simple meson.build
file could be:
project('myprogram project', 'vala', 'c')
dependencies = [
dependency('glib-2.0'),
dependency('gobject-2.0'),
]
sources = []
subdir('src')
executable('myprogram', sources, dependencies: dependencies)
Save this in the project directory and then in the src
directory a meson.build
file that explicitly lists the source files:
sources += files(
'a.vala',
'b.vala',
)
Note that subdir()
uses the meson.build
file in src
directory to to append the source files to the sources
variable.
To build the project, first set up the build directory. Do this from the project directory:
meson setup builddir
Then build the project:
ninja -C builddir
You may need to use ninja-build
for Red Hat based distributions like Fedora. The executable, myprogram
, is in the builddir
.
If you change a source file then ninja -C builddir
will rebuild with no need to delete anything.
As your project grows you will find Meson is far more manageable than using Makefiles.