Huge bunch of undefined references when compiling example application

Hi,

I hope this is the right section to ask this…
I installed the latest gtk(4) from the git repo (ninja install) and am now trying to compile the example application (/examples/hello-world.c) with this command:

gcc `pkg-config gtk4 --cflags` `pkg-config gtk4 --libs` ./hello-world.c

but all I get is:

/usr/bin/ld: /tmp/ccVkr7vc.o: in function `print_hello':
test.c:(.text+0x21): undefined reference to `g_print'
/usr/bin/ld: /tmp/ccVkr7vc.o: in function `activate':
test.c:(.text+0x44): undefined reference to `gtk_application_window_new'
/usr/bin/ld: test.c:(.text+0x4d): undefined reference to `gtk_window_get_type'
/usr/bin/ld: test.c:(.text+0x5f): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: test.c:(.text+0x6e): undefined reference to `gtk_window_set_title'
/usr/bin/ld: test.c:(.text+0x73): undefined reference to `gtk_window_get_type'
/usr/bin/ld: test.c:(.text+0x85): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: test.c:(.text+0x97): undefined reference to `gtk_window_set_default_size'
/usr/bin/ld: test.c:(.text+0xa6): undefined reference to `gtk_box_new'
/usr/bin/ld: test.c:(.text+0xaf): undefined reference to `gtk_window_get_type'
/usr/bin/ld: test.c:(.text+0xc1): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: test.c:(.text+0xd3): undefined reference to `gtk_window_set_child'
/usr/bin/ld: test.c:(.text+0xdf): undefined reference to `gtk_button_new_with_label'
/usr/bin/ld: test.c:(.text+0x10e): undefined reference to `g_signal_connect_data'
/usr/bin/ld: test.c:(.text+0x12c): undefined reference to `gtk_window_destroy'
/usr/bin/ld: test.c:(.text+0x13b): undefined reference to `g_signal_connect_data'
/usr/bin/ld: test.c:(.text+0x140): undefined reference to `gtk_box_get_type'
/usr/bin/ld: test.c:(.text+0x152): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: test.c:(.text+0x164): undefined reference to `gtk_box_append'
/usr/bin/ld: test.c:(.text+0x170): undefined reference to `gtk_widget_show'
/usr/bin/ld: /tmp/ccVkr7vc.o: in function `main':
test.c:(.text+0x197): undefined reference to `gtk_application_new'
/usr/bin/ld: test.c:(.text+0x1c6): undefined reference to `g_signal_connect_data'
/usr/bin/ld: test.c:(.text+0x1cb): undefined reference to `g_application_get_type'
/usr/bin/ld: test.c:(.text+0x1dd): undefined reference to `g_type_check_instance_cast'
/usr/bin/ld: test.c:(.text+0x1f1): undefined reference to `g_application_run'
/usr/bin/ld: test.c:(.text+0x200): undefined reference to `g_object_unref'

What am I doing wrong, any suggestions?

Tuxifan

1 Like

Where did you install GTK4? Which prefix did you use?

What does happen when you do:

pkg-config --cflags gtk4

by itself?

Nevermind. Tried clang instead of gcc and that solved the issue. :slight_smile:
Edit: thx for the prompt response anyways

The issue is still present, even if by accident it is working with clang. As shown by the examples in the official documentation the dependencies (i.e. pkg-config --libs gtk-4.0) must be put after your source file. The order is significant.

Probably depends on your pkg-config implementation. But this works here:

gcc -g -o hello-world hello-world.c `pkg-config --cflags --libs gtk4`

But then, this works too:

gcc `pkg-config gtk4 --cflags` `pkg-config gtk4 --libs` ./hello-world.c

This is by accident, and in the past I personally ignored the arguments order for years until an Ubuntu update broke my builds. Citing the gcc documentation:

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

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