问题
I've a C GUI application (in GTK+2.0) that I used to link as follows :
gcc -O2 -std=gnu99 -pipe -Wall -lm `pkg-congig gtk+-2.0 `pkg-config --libs gtk+-2.0` -o exec a.o b.o c.o
which eventually converted into :
gcc -O2 -std=gnu99 -pipe -Wall -lm -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/glib-2.0 -I/usr/lib/x86_64 linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lglib-2.0 -lcairo -lgtk-x11-2.0 -lglib-2.0 -o exec a.o b.o c.o
It was until Ubuntu 12.04.
When I tried to link the files in the same way in Ubuntu 12.04 as well, it showed the following error :
/usr/bin/ld: b.o: undefined reference to symbol 'gdk_color_parse'
/usr/bin/ld: note: 'gdk_color_parse' is defined in DSO /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libgdk-x11-2.0.so so try adding it to the linker command line
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libgdk-x11-2.0.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
If anyone has any idea about this please share.
回答1:
With newer versions of GCC/binutils, linker flags have to come last because the linker looks ahead only for undefined symbols. Try
gcc -O2 -std=gnu99 -pipe -Wall a.o b.o c.o -lm `pkg-config --libs gtk+-2.0` -o exec
instead.
来源:https://stackoverflow.com/questions/12434568/linking-error-in-gtk2-application-in-ubuntu-12-04