Problem including gtk-4.0

Hi, i am using vscode and i am trying to test the example in the https://www.gtk.org/ home page.

// Include gtk
#include <gtk/gtk.h>

static void on_activate(GtkApplication *app)
{
    // Create a new window
    GtkWidget *window = gtk_application_window_new(app);
    // Create a new button
    GtkWidget *button = gtk_button_new_with_label("Hello, World!");
    // When the button is clicked, close the window passed as an argument
    g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_close), window);
    gtk_window_set_child(GTK_WINDOW(window), button);
    gtk_window_present(GTK_WINDOW(window));
}

int main(int argc, char *argv[])
{
    // Create a new application
    GtkApplication *app = gtk_application_new("com.example.GtkApplication",
                                              G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
    return g_application_run(G_APPLICATION(app), argc, argv);
}

Now i am getting an error that i cannot find gtk/gtk.h
So i changed the include to #include <gtk-4.0/gtk/gtk.h>
Note that i have several gtk versions in /usr/include, i have gtk-2.0, gtk-3.0 and gtk-4.0.

Now it asks me for gtk/css/gtkcss.h so i included it.

#include <gtk-4.0/gtk/css/gtkcss.h>
#include <gtk-4.0/gtk/gtk.h>

Now the compiler asks me for glib.h

gcc -Wall --static -o target/app src/main.c 
In file included from src/main.c:2:
/usr/include/gtk-4.0/gtk/css/gtkcss.h:30:10: fatal error: glib.h: No such file or directory
   30 | #include <glib.h>
      |          ^~~~~~~~
compilation terminated.
make: *** [makefile:8: target/app] Error 1

So i included it:

#include <glib-2.0/glib.h>
#include <gtk-4.0/gtk/css/gtkcss.h>
#include <gtk-4.0/gtk/gtk.h>

gcc -Wall --static -o target/app src/main.c 
In file included from src/main.c:1:
/usr/include/glib-2.0/glib.h:30:10: fatal error: glib/galloca.h: No such file or directory
   30 | #include <glib/galloca.h>
      |          ^~~~~~~~~~~~~~~~
compilation terminated.
make: *** [makefile:8: target/app] Error 1

This bastard keeps asking for includes, Is there a solution for this ?

You need to be able to tell VSCode to use the output of pkg-config --cflags gtk4 for the compiler arguments, and pkg-config --libs gtk4 for the linker arguments.

You should probably ask on a VSCode user forum.

1 Like

I have compiled it successfully using this command:

gcc -Wall -o target/app src/main.c `pkg-config --cflags --libs gtk4`

The problem here when trying to compile it statically:

gcc -Wall --static -o target/app src/main.c `pkg-config --cflags --libs gtk4`

I get this error:

/usr/bin/ld: cannot find -lgtk-4
/usr/bin/ld: cannot find -lpangocairo-1.0
/usr/bin/ld: cannot find -lpango-1.0
/usr/bin/ld: cannot find -lgdk_pixbuf-2.0
/usr/bin/ld: cannot find -lgraphene-1.0
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/libgio-2.0.a(glocalfileinfo.c.o): in function `_g_local_file_info_get':
(.text+0x3824): warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/libglib-2.0.a(gutils.c.o): in function `g_get_user_database_entry':
(.text+0x277): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: (.text+0xe0): warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/libgio-2.0.a(glocalfileinfo.c.o): in function `lookup_uid_data':
(.text+0x11b4): warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/libgio-2.0.a(ginetsocketaddress.c.o): in function `g_inet_socket_address_new_from_string':
(.text+0x9ac): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/libgio-2.0.a(gnetworkaddress.c.o): in function `g_network_address_parse':
(.text+0x1813): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: (.text+0x182d): warning: Using 'endservent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status
make: *** [makefile:8: target/app] Error 1

Any ideas how to solve that ?

First of all, static linking in GTK isn’t at all supported. It might work, or it might not.

Additionally, you need to tell pkg-config that it needs to list all transitive dependencies and you must have static builds of everything. You can try using pkg-config --static --cflags --libs gtk4 instead.

In general, once you get into this kind of work, you really want to use a build system; the GTK project recommends Meson.

3 Likes

I did that:

gcc --static -Wall -o target/app src/main.c  `pkg-config --static --cflags --libs gtk4`

but got that error:

/usr/bin/ld: cannot find -lgtk-4
/usr/bin/ld: cannot find -lepoxy
/usr/bin/ld: cannot find -lGL
/usr/bin/ld: cannot find -lEGL
/usr/bin/ld: cannot find -lxkbcommon
/usr/bin/ld: cannot find -lwayland-egl
/usr/bin/ld: cannot find -lwayland-client
collect2: error: ld returned 1 exit status

I came across this question and will try following the solution there.

Greetings :wave:

Do you know the piece of code you are playing with is just and excerpt used to compare a “Hello world” GTK program looks like in C, JavaScript, Perl, Python, Rust and Vala.

The “Hello world” example that it’s meant to be compiled is in the documentation on the very page you posted (GTK’s home page). There you can find the “Learn GTK” button:

That will present you many resources to learn GTK. One of those resources is the Getting Started where on the bottom you will find the “real” Hello World meant to be compiled.

And in the Hello World page there’s a note:

Note: We assume that you have GTK, its dependencies and a C compiler installed and ready to use. If you need to build GTK itself first, refer to the Compiling the GTK libraries section in this reference.

So, in order to build the Hello World program in the link pointed by the Getting Started page, and even possibly the excerpt you decided to use, you need to setup a development environment. The GNOME Developer is your friend here, the Newcomers page is your friend here, if you go the VSCode route, the internet is your friend too.

You see, it’s a natural progression. Reading what’s in the page and following the links will carry you step by step, but if you choose the VSCode way you’ll have to know how to adapt the instructions.

My last bit of advice is about you trying to static link the “Hello World” excerpt program. You are trying to run before learning how to walk. As Emmanuele Bassi already said, and I reiterate here, it’s not something that’s supported and doesn’t give you any guarantees that it will work. Choosing going down this road is to be pretty much on your own and this forum won’t be of much help. :slightly_frowning_face:

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.