Unable to compile GTK+ application

1

I'm trying to compile a simple GTK app for testing on a Trisquel 7 machine. The packages are derived from Ubuntu 14.04 LTS. I have libgtk2.0-dev installed, but when I try to compile, this is the result.

$ gcc -Wall -Wextra -std=c11 `pkg-config --libs --cflags gtk+-2.0` main.c
a_out-main.o: In function `main':
/home/mac/c/main.c:7: undefined reference to `gtk_init'
/home/mac/c/main.c:9: undefined reference to `gtk_window_new'
/home/mac/c/main.c:10: undefined reference to `gtk_window_get_type'
/home/mac/c/main.c:10: undefined reference to `g_type_check_instance_cast'
/home/mac/c/main.c:10: undefined reference to `gtk_window_set_title'
/home/mac/c/main.c:11: undefined reference to `g_type_check_instance_cast'
/home/mac/c/main.c:11: undefined reference to `gtk_window_set_default_size'
/home/mac/c/main.c:12: undefined reference to `g_type_check_instance_cast'
/home/mac/c/main.c:12: undefined reference to `gtk_window_set_position'
/home/mac/c/main.c:13: undefined reference to `gtk_widget_show'
/home/mac/c/main.c:15: undefined reference to `g_type_check_instance_cast'
/home/mac/c/main.c:15: undefined reference to `gtk_main_quit'
/home/mac/c/main.c:15: undefined reference to `g_signal_connect_data'
/home/mac/c/main.c:18: undefined reference to `gtk_main'

I have checked the output of pkg-config --libs --cflags gtk+-2.0, and it looks correct, but for some reason, it's not being correctly linked.

2mac

Posted 2014-10-15T14:30:51.143

Reputation: 235

Answers

3

Put the backticks containing the pkg-config command at the end of the line. For some reason, gcc only reads it correctly if it's last. I had the exact same problem today trying to build examples from the GTK repo. The line in their makefiles,

$(CC) -o $(@F) $(LIBS) $(OBJS)

ought to be

$(CC) -o $(@F) $(OBJS) $(LIBS)

instead.

VaelynPhi

Posted 2014-10-15T14:30:51.143

Reputation: 46

1

I believe the problem is that the linker is single-pass. I.e., it must see a reference before it finds its definition. Thus, the pkg-config bit, where the definitions are pointed out, must come after main.c, where the references are.

massemanet

Posted 2014-10-15T14:30:51.143

Reputation: 11

This is better than the "for some reason" of the other answer. And, indeed, although pkg-config --blah file.c works for many simple cases, it doesn't always - e.g. it works for me on Debian with the pkg-config before a single test.c, but not with the MSYS2 mingw-w64 shell. – underscore_d – 2016-05-31T13:58:24.407