Jump to content

Vala Programming/Concepts/Basic

From Wikibooks, open books for an open world

Source files

[edit | edit source]

valac is the Vala compiler, but it does not only accept Vala: it also accepts Genie (with python-like syntax, extension: .gs), C, and .vapi (Vala API) files. You can declare a low-level function in C, create an object with this function as method in Vala, and use both in a Genie file: you will get a ready-to-run executable.

How does the Vala Compiler work

[edit | edit source]

The main function of Vala was to be a modern object-oriented programming language for GNOME. Both Vala and Genie sources are processed into C code, which is compiled.

i.e., in this example:

$ valac func.c obj.vala prog.gs -o program

Valac generates obj.vala.c from obj.vala and prog.gs.c from prog.gs. Then, Valac runs GCC (with many -I arguments, automatically generated) to compile all files to object code, and finally ld is called by gcc: we will get a native executable, "program", from a mix of C, Vala and Genie source code.

Arguments

[edit | edit source]

Of course, the typical arguments, like --version and --help work with Valac. It also inherits the -c (compile to .o, no linking) argument from GCC.

Other useful arguments:

  • -C: convert to C code
  • --cc=COMMAND: use COMMAND as C compiler
  • -Xopt: pass -opt as argument to the C compiler (ex: -Xcc=O3 sets GCC's optimizer to level 3)
  • --pkg PNAME: add packages from pkg-config (like -pkg:PNAME in Mono mcs or `pkg-config PNAME --cflags --libs` in gcc)


A complete example (program using GTK2 & goocanvas, some threading, etc.):

  • valac --pkg gtk+-2.0 --pkg goocanvas --pkg glib-2.0 --thread --pkg gee-1.0 --debug --Xcc=-O3 gtk_goocanvas.vala

Namespaces

[edit | edit source]

TODO Explain how to structure a project of a program or library written in Vala.