Scooter
Scooter

Reputation: 7061

Vala 'hello world' example gives warning about g_type_init

Compling this "hello world" example found at the Gnome Wiki Vala Tutorial gives me a warning.

class Demo.HelloWorld : GLib.Object {

    public static int main(string[] args) {

        stdout.printf("Hello, World\n");

        return 0;
    }
}



/home/scooter3/code/vala/hello_world.vala.c:55:2: warning: ‘g_type_init’ is deprecated (declared at /usr/include/glib-2.0/gobject/gtype.h:667) [-Wdeprecated-declarations]
  g_type_init ();

I would like to either modify the program or install some other version of the a lib in order to get rid of the warning.

Upvotes: 1

Views: 997

Answers (2)

Jens Mühlenhoff
Jens Mühlenhoff

Reputation: 14873

As you have already discovered, it's possible to have multiple version of valac installed on the same system.

You could explicitly compile with valac-0.30 or valac-0.20 (etc.). Your Linux distribution (apparently Ubuntu) has a package that manages a symlink from /usr/bin/valac to one of the installed vala compilers.

Explanation for the concrete warning you were seeing:

Older versions of glib / gobject (which is the basic OOP system used by Vala) needed this call to g_type_init in order to work and the Vala compiler tries to keep the code compatible with older versions as well.

You can explicitly disable this behavior with --target-glib=2.44 (or whatever minimum version of glib you want to support).

However in newer valac versions this warning is fixed by using the GLIB_CHECK_VERSION macro, i.e.:

#if !GLIB_CHECK_VERSION (2,35,0)
        g_type_init ();
#endif

This check avoids calling g_type_init if glib is at least version 2.35.

For C compiler warning in general:

Only very trivial Vala programs don't generate C compiler warnings, you have to carefully scan compiler warnings for their origin (valac or gcc). Usually you only have to care about warnings outputed by valac, not those by your C compiler (like gcc).

There is an exception to this rule if you write .vapi files where the warnings of the C compiler matter a lot more and can point to errors in the vapi files. (vapi files are used to make C libraries accessable to Vala).

Upvotes: 1

Scooter
Scooter

Reputation: 7061

I must have previously installed vala, as valac --version gave

Vala 0.20.1

I thought I had installed everything with

sudo add-apt-repository ppa:vala-team 
sudo apt-get install libgee-0.8 vala-0.30 valadoc

but I needed to also do

sudo apt-get install valac

to get the Vala 0.30.0 version of the compiler. Once I did that the warning went away.

Upvotes: 0

Related Questions