Vala + Autotools example

As a part of learning great Vala language I decided to implement the simplest possible example of Vala console application e.g. a simple calculator program that parses user input and evaluates the expression.

The project is going to have:

  • Unit tests that tests API level functions.
  • A usage example of libraries that can be used both for unit tests and for the program itself.
  • Small and readable Autotools code. The project should be reusable by anyone else as a skeleton for their own project.

The code of the project you can find at my github page.

I am not going to describe Autotools plumbing code. It is pretty much the same as any other Autotools based project has. I recommend to read following article if you are interested in the Autotools details.

Let’s look at the Vala-specific code instead.

  • The key file for autoconf tool.It has several interesting pieces:

AM_PROG_VALAC – Autotools already has Vala support and the given macros enables it. After you added it Automake knows what to do with *.vala source files – it invokes valac compiler.

pkg_modules="glib-2.0 >= 2.24.1
             gobject-2.0 >= 2.24.1"

This code checks in your system libraries needed to compile Vala C files. It also evaluates paths to header and shared library files – later these variables will be used for compilation.

  • src/calculator.vala is a core file that contains most of the calculator logic. Later we are going to test this functionality.
  • src/main.vala this is thin wrapper for the calculator library. It has main() method and this file is needed for creating end-used console application.
  • src/ This is the most interesting file in the project. Let’s look at it closely.
bin_PROGRAMS = calculator

Here we declare that this directory has 1 library and 1 binary file.

libcalculator_la_SOURCES = calculator.vala
libcalculator_la_VALAFLAGS = --library calculator -H calculator.h
Calculator library consists of 1 file – calculator.vala but you can as many *.vala files as you want. The second line declares that make should generate calculator.vapi file and *.h that is also needed for compilation.  *.vapi file is kinda ‘header’ file for Vala compier the *.vapi file contains Vala classes declaration, but does not have any implementation. So the library generates 3 files – *.la, *.vala, *.h.
calculator_SOURCES = main.vala
calculator_VALAFLAGS = calculator.vapi

This generates output binary file, our console application that we develop. Its source file is main.vala – wrapper around the library. _LDADD adds calculator library as a dependency, _VALAFLAGS  passes *.vapi file to valac compiler.

  • test/calculator_test.vala. This is a unit (aka API level) test
  • test/

Run ./ and then ‘make’ to build ./src/calculator console application. If you run ‘make check’ you’ll see test results

make  check-TESTS
/calculator/add: OK
/calculator/minus: OK
/calculator/multiply: OK
PASS: calculator-test

PS Things that I might add in the future:

  • Valadoc documentation generation. It is nice to have API in readable format so you can upload it to the project server.
  • CMake example. I don’t have a lot of experience in autotools so in this project I learned Autotools in addition to Vala language. As of my taste Autotools have a lot of black magic. I still do not understand how the build system works for my project. Some people say that CMake easier to learn that Autotools. So similar example for CMake could be a good lesson as well.

3 responses to this post.

  1. Posted by Javier Jardón on January 11, 2011 at 08:46

    Thanks for the post. It has been useful for me.
    I’d suggest you to use non-recursive automake, more info here:

    Also, maybe you can try waf to build your project:



  2. Hi, Javier.

    >> maybe you can try waf to build your project
    There are several examples of Waf/Vala integration. You might be interested in this on The wiki page is not very useful but it points to vala-project-template project sources. You can also check demo from waf repository


  3. There is actually yet another build tool that looks interesting called premake

    Unfortunately there is no any Vala support for it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: