As a total newbie in coding, I’m trying to understand why I can’t compile gtk with vala.
when I try to run some code (copy/paste from the tutorial page on the GTK’s site), gnome-builder’s terminal run this:
[user@hostname src]$ valac --pkg gtk±4.0 main.vala
error: Package `gtk±4.0’ not found in specified Vala API directories or GObject-Introspection GIR directories
Compilation failed: 1 error(s), 0 warning(s)
Wouldn’t be gnome-builder “ready to use” for compiling GTK? Do I have to set some environment variable in the IDE?
[user@hostname src]$ valac --pkg gtk4 main.vala
main.vala:25.5-25.23: error: 1 extra arguments for `void Gtk.init ()'
25 | Gtk.init (ref args);
| ^~~~~~~~~~~~~~~~~~~
main.vala:29.5-29.23: error: The name `border_width' does not exist in the context of `Gtk.Window' (gtk4)
29 | window.border_width = 10;
| ^~~~~~~~~~~~~~~~~~~
main.vala:27.9-27.14: note: `window' was declared here
27 | var window = new Window ();
| ^~~~~~
main.vala:30.5-30.26: error: The name `window_position' does not exist in the context of `Gtk.Window' (gtk4)
30 | window.window_position = WindowPosition.CENTER;
| ^~~~~~~~~~~~~~~~~~~~~~
main.vala:27.9-27.14: note: `window' was declared here
27 | var window = new Window ();
| ^~~~~~
main.vala:32.5-32.26: error: The name `connect' does not exist in the context of `Gtk.Window.destroy'
32 | window.destroy.connect (Gtk.main_quit);
| ^~~~~~~~~~~~~~~~~~~~~~
gtk4.vapi:13161.3-13161.21: note: `destroy' was declared here
13161 | public void destroy ();
| ^~~~~~~~~~~~~~~~~~~
main.vala:39.5-39.14: error: The name `add' does not exist in the context of `Gtk.Window' (gtk4)
39 | window.add (button);
| ^~~~~~~~~~
main.vala:27.9-27.14: note: `window' was declared here
27 | var window = new Window ();
| ^~~~~~
main.vala:40.5-40.19: error: The name `show_all' does not exist in the context of `Gtk.Window' (gtk4)
40 | window.show_all ();
| ^~~~~~~~~~~~~~~
main.vala:27.9-27.14: note: `window' was declared here
27 | var window = new Window ();
| ^~~~~~
main.vala:42.5-42.12: error: The name `main' does not exist in the context of `Gtk' (gtk4)
42 | Gtk.main ();
| ^~~~~~~~
gtk4.vapi:6452.1-6452.13: note: `Gtk' was declared here
6452 | namespace Gtk {
| ^~~~~~~~~~~~~
Compilation failed: 7 error(s), 0 warning(s)
P.S.: The command for compiling that I posted in the first post is taken from this page
You’re trying to compile GTK3 code with GTK4. It will never work. You need to either write GTK3 code to compile with GTK3, or write GTK4 code to compile with GTK4.
But I specified gtk4 … so how/where I have to setup?
Please, remember that I’m not programmer as I literally specified since my first post .I’m a total newbie that do this in the day off. Please do not post answers to much geeky/nerd or too short because I’m tyring to learn all by my self. the GTK doc is complete but not so clear . I just copy/paste the sample from the doc page, both the hello world window that the compiling line and it does not works. So, I’m ignorant, but in the meantime, there’s something not so clear in the documentation …
So, where I have to check to set the proper library?
You can specify “I want to build with GTK4”, but code written for GTK3 can only be built with GTK3. You copy-pasted some code from somewhere, and that code is written using GTK3. The same place that gave you the code should also include instructions on how to compile it.
Where did you copy the code from? The “tutorial page on the GTK site” only has C code, not Vala code, so you got that code from somewhere else. The page on Vala on the GTK website shows GTK4 code and how to build it with GTK4.
Note: These examples require GTK+ 3.x installed, e.g.
Fedora >= 15: gtk3-devel
openSUSE >= 11.4: gtk3-devel
Ubuntu >= 11.04: libgtk-3-dev
It also says:
$ valac --pkg gtk+-3.0 gtk-hello.vala
So the wiki is already specifically telling you:
the code is written using GTK3
how to compile the code with GTK3
You cannot compile GTK3 code against GTK4: the two versions are incompatible. If you want to write GTK4 code, you will need to use other examples. There is a GTK4 example on the same wiki.
With the code of the page you linked, now it returns this:
[user@hostname src]$ valac --pkg gtk4 main.vala
/home/user/.../main.vala.c: In function ‘_vala_main’:
/home/user/.../main.vala.c:152:9: warning: ‘G_APPLICATION_FLAGS_NONE’ is deprecated: Use 'G_APPLICATION_DEFAULT_FLAGS' instead [-Wdeprecated-declarations]
152 | _tmp0_ = gtk_application_new ("com.example.GtkApplication", G_APPLICATION_FLAGS_NONE);
| ^~~~~~
In file included from /usr/include/glib-2.0/gio/giotypes.h:30,
from /usr/include/glib-2.0/gio/gio.h:28,
from /usr/include/gtk-4.0/gtk/css/gtkcsssection.h:24,
from /usr/include/gtk-4.0/gtk/css/gtkcss.h:36,
from /usr/include/gtk-4.0/gtk/gtk.h:29,
from /home/user/.../main.vala.c:28:
/usr/include/glib-2.0/gio/gioenums.h:1540:3: note: declared here
1540 | G_APPLICATION_FLAGS_NONE GIO_DEPRECATED_ENUMERATOR_IN_2_74_FOR(G_APPLICATION_DEFAULT_FLAGS),
| ^~~~~~~~~~~~~~~~~~~~~~~~
Anyway from
to
there’s no difference but the gtk4 (that I specified since the firts attempt). It compiles and the window appears but, as above, I can’t understand why something is refered to glib2.0. Then I cannot understand the reference to the deprecated flag.
It tells you that the flags value G_APPLICATION_FLAGS_NONE is correct but will be deprecated in the future in favor of the new name G_APPLICATION_DEFAULT_FLAGS
You can use the new flag name in your code to hide the warning. This new name is only available from GLib 2.74, so if you plan to be compatible with older version (which I don’t think, it’s already quite old) then you can keep G_APPLICATION_FLAGS_NONE for some time.