Getting Started With Vala
Vala is an object oriented programming language that was designed specifically for GTK+ and GNOME development. Vala’s syntax is similar to C# but it compile to C code using the GObject system which let you use the GNOME ecosystem easily.
Let’s start with writing our first program in Vala which will of course be to print Hello World to the console. Vala is objected oriented but the entry point of the program is the main function, not method. In the main function we will use the function print to write hello world on the screen.
void main () {
print ("Hello World\n");
}
To compile this program, install the Vala compiler from your distribution
package manager and run the compiler using the valac
command with the
name of the Vala file you created. If you called your file
hello-world.vala
the command will look like that and you will get a
hello-world
file that you can execute like any other executeable file.
valac hello-world.vala
So we wrote our first program in Vala but printing to the console is a bit boring. You probably want to learn Vala so you could make awesome GUI applications with GTK+. Let’s start with writing Hello World again but this time in a GTK+ window. This is the complete Hello World program but we will go line by line and explain it.
using Gtk;
int main (string[] args) {
Gtk.init (ref args);
var window = new Window ();
window.title = "Hello World";
window.destroy.connect (Gtk.main_quit);
window.show_all ();
Gtk.main ();
return 0;
}
So what is going on in this example? The first thing we have to do in
each Vala file that will use GTK+ is to import with the using
keyword.
The next line in our program is the main function again, but this time
it’s signature have changed. It require a list of strings argument
called args
and return an integer. The args
argument is required for
the initialization of GTK+ but we won’t go into details about it in this
post. The return value of the function is the exit code of the process,
0 indicate a successful run and any other value indicate an error.
In the fourth line we pass the args
argument we talked about to the
Gtk.init
function. This function initialize everything needed to
operate the toolkit.
In line 6-9 we initialize a new instance of the Window class (using the
new keyword). Than we set the window title to Hello World, set the
destroy signal to call Gtk.main_quit
and lastly call the
show_all
method to display the window.
Showing the window won’t actually show it without the call to
Gtk.main
on line 11 because without Gtk.main
the program will
exit immediately after calling the show_all
method.
The last line of our function is return 0
which will tell the parent
process and the operating system that the process was executed
successfully.
Now that we know what this program does we want to compile it but if we
will try to compile it with the command from the previous example we
will get an error that namespace Gtk could not be found. That is
because we didn’t tell the compile to use the Gtk package1. To do
that we will run the compiler with the --pkg
flag like that:
valac --pkg gtk+-3.0 window.vala
That’s it, you can now run the program like you did the in previous example and see a small window with the title Hello World pop up.
-
You also need to install the development package for GTK+. ↩︎