2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
GunJack
  • 1,928
  • 2
  • 22
  • 35

3 Answers3

5

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:

  1. Tabs are used for indentation, in case that didn't come across from the example
  2. You will need to delete the output file to rebuild it when you make any changes to the source files
  3. A wildcard is used to include all Vala source files in the src directory

Many 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.

AlThomas
  • 4,169
  • 12
  • 22
3

I guess meson is way to build vala nowadays. Check out https://mesonbuild.com/Vala.html

aeldemery
  • 81
  • 1
  • 2
  • Very short contributions are discouraged here. See the effort from AlThomas to give you an idea of what makes a good answer. – halfer Apr 25 '20 at 13:49
0

Here's a vala Makefile that works perfectly, but I recommend using meson.build rather than makefile, it's very easy to learn and much more efficient :)

SRC=main.vala hello.vala
SRC_C=$(SRC:.vala=.c)
OBJ=$(SRC_C:.c=.o)
PKG=glib-2.0 gio-2.0
NAME = exe

all: $(NAME) 

$(NAME): $(OBJ)
        @echo "[Link]:"
        gcc ```pkg-config --libs $(PKG)``` $(OBJ) -o $(NAME)

$(SRC_C):
        @echo "[Compile vala to c]:"
        valac $(SRC) $(addprefix --pkg=,$(PKG)) -C

# [Compile c to Object]
%.o: %.c
        gcc ```pkg-config --cflags $(PKG)```  $< -c -o $@

clean:
        rm -rf $(OBJ)
        rm -rf $(SRC_C)

fclean: clean
        rm -rf $(NAME)

re: fclean all
hydral
  • 1
  • 2