Vala is the programming language but the interesting one. After compilation, it produces the C code and then this C code further fed to C compiler to get the final output.
Create a dump.vala
file and write the following code. Nothing is new in this code and we already write this same code in previous articles.
public class Hello { public string name; public Hello(string name) { this.name = name; } public void greet() { stdout.printf("hello, %s\n", this.name); } } int main(string[] args) { string name = "world"; Hello hello = new Hello(name); hello.greet(); return 0; }
Let's run this file using Vala compiler but with -C
option.
$ valac -C dump.vala
This -C
flag outputs the C code into the file with same name as the Vala file i.e. dump.c
file. Open this dump.c
file and you should see the following code.
/* dump.c generated by valac 0.56.3, the Vala compiler * generated from dump.vala, do not modify */ #include <glib-object.h> #include <stdlib.h> #include <string.h> #include <glib.h> /* ... more core ...*/ /* ... more core ...*/ /* ... more core ...*/ int main (int argc, char ** argv) { return _vala_main (argv, argc); }
You don't have to worry about this C code and even you don't need to know this C code produced by Vala compiler. I show this step to you to give some background information such as Vala use GLib Object and GLib libraries extensively as you can see from the header files of this C code. Some of the code and syntax you'll going to use in Vala depends on these libraries and C in general. So, don't be surprise!
Apart from this, even though Vala is general purpose programming language, it is mainly used to develop the GUI application for the GNU/Linux system using GTK library as it has tight integration with GTK library and the ecosystem. elementary OS extensively use the Vala language to build most of their applications including the Pantheon, their desktop environment.
Vala gives nice abstraction like higher level languages such as Java and C# to make desktop application using GTK which is written in C language. That being said, let's continue with Vala language and build the hello, world desktop application using GTK library (GTK4).