GTK3 program no longer compiles - GCC "undefined reference" at link time - gcc

So I was merrily learning GTK+ programming with 3.0 in Ubuntu 11.04, and even got a working ruler program running. It compiled with my makefile perfectly.
I took some time away from it, upgraded to 11.10, and now have come back to it. But, alas, when I try to build the program, I get a lot of "undefined reference" errors to pretty much every gtk call in my program. Compiling is fine - the issue is at link time.
The project can be found here: https://github.com/zjmichen/ruler

I've tested it and it compiled fine.
superman#superman-mint ~/work/zjmichen-ruler-e783fe1 $ make
gcc -c -Wall `pkg-config --cflags --libs gtk+-3.0` main.c
gcc -c -Wall `pkg-config --cflags --libs gtk+-3.0` window.c
gcc -c -Wall `pkg-config --cflags --libs gtk+-3.0` graphics.c
gcc -c -Wall `pkg-config --cflags --libs gtk+-3.0` mouse.c
gcc `pkg-config --cflags --libs gtk+-3.0` main.o window.o graphics.o mouse.o -o zruler
superman#superman-mint ~/work/zjmichen-ruler-e783fe1 $
But this was on Linux Mint. Here you've got the arguments produced by pkg-config
-pthread -DGSEAL_ENABLE -I/usr/include/atk-1.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/freetype2 -I/usr/include/libpng12 -I/usr/include/gtk-3.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pixman-1 -pthread -L/usr/lib/x86_64-linux-gnu -lgtk-3 -lgdk-3 -latk-1.0 -lcairo-gobject -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lm -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0
I've also did a quick test on Ubuntu 11.10 and indeed it doesn't compile. I'll take a better look this evening.
So I did test it on Ubuntu and changed the line 14 in the makefile to
$(CC) $(OBJS) $(GTKFLAGS) -o $(NAME)
this changes the order of object files and libraries that are being linked. I have no idea why this problem occurs on the new Ubuntu. Maybe it is because it is a different version of gcc. On my linux mint gcc is version 4.5.2 on ubuntu 11.10 it is 4.6.1

Unfortunately I currently don't have access to a running Linux machine with GTK3.0 installed, but looking at your code I noticed the inclusion of X11/xlib.h in main.c. Since you are not using X11 code anywhere in your code and you are using 'pkgconfig ... gtk+-3.0' instead of gtk+-X11-3.0, I assume you could remove that include statement. If you do need the X11 libraries the make sure it is listed in the compiler flags. I hope this helps :)

Related

What is the difference between -Wl,--export-dynamic and -Wl,-export-dynamic (single vs double-dash)?

A user of xnec2c was trying to build on OSX with clang and got this error:
gcc --pedantic -Wall -std=gnu11 -O2 -g -Wformat -Werror=format-security -fpie -Wno-overlength-strings -DGTK_DISABLE_SINGLE_INCLUDES -DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED -DGSEAL_ENABLE -DGDK_PIXBUF_DISABLE_DEPRECATED -DG_DISABLE_DEPRECATED -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fno-honor-nans -fno-signed-zeros -fno-math-errno -Wl,--export-dynamic -Wl,--as-needed -o xnec2c main.o mathlib.o measurements.o interface.o callbacks.o console.o callback_func.o calculations.o cmnd_edit.o geom_edit.o gnuplot.o draw.o draw_structure.o draw_radiation.o fields.o fork.o geometry.o ground.o xnec2c.o input.o matrix.o utils.o nec2_model.o network.o optimize.o plot_freqdata.o radiation.o rc_config.o shared.o somnec.o xnec2c-resources.o -L/opt/local/lib -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl -lpthread -lm
ld: unknown option: --export-dynamic
clang: error: linker command failed with exit code 1 (use -v to see invocation)
(FYI: GTK Builder needs the export-dynamic linking flag.)
This discussion suggests making the double-dash a single dash:
-AM_LDFLAGS = -Wl,--export-dynamic
+AM_LDFLAGS = -Wl,-export-dynamic
However the user found that, actually, it needs a single dash and an underscore (maybe, still having trouble that could be related, but at least ld stopped barking about the option):
-AM_LDFLAGS = -Wl,--export-dynamic
+AM_LDFLAGS = -Wl,-export_dynamic
Questions:
Does single-dash -Wl,-export-dynamic work in both GCC and Clang?
Whats with the underscore version as -Wl,-export_dynamic?
How compatible is that option with older (and newer) versions of the GCC/Clang stacks?
Other considerations or best practice?
Both GNU ld and LLVM lld take --export-dynamic flag
ld that ships with OS X takes -export_dynamic flag
This has nothing to do with gcc or clang, these only pass the flag along to the linker

How to cgo link to libraries built with LLVM's libc++

I would like to CGO link my go program to a c++ static library linked to LLVM's libc++. With GNU's libstdc++, everything works fine.
I am calling go compiler as follows:
CGO_CXXFLAGS="-stdlib=libc++ -I/usr/lib/clang/12.0.0/include -I/usr/local/include -I/usr/include" CGO_LDFLAGS="-stdlib=libc++ -L/usr/lib/clang/12.0.0/lib -L/usr/local/lib -L/usr/lib -L/usr/lib/x86_64-linux-gnu -lpthread -lrt -lc++ -lm -lc" go test --ldflags '-extldflags "-static"' -c -a .
I was missing -lc++ -lc++abi.

FLTK compile error with gcc

I am trying to compile my first FLTK file. First I compiled it by gcc with -lfltk option, and I got this error:
/usr/bin/ld: /tmp/ccX7sPxQ.o: undefined reference to symbol '__gxx_personality_v0##CXXABI_1.3'
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Why do I get this error message? If I use g++ instead, it just compiles it.
edit.
fltk-config --cxxflags returns:
-I/usr/include/cairo -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
-I/usr/include/freetype2 -I/usr/include/cairo -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 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT
-lfltk is not enough to build a minimal FLTK-based application.
I strongly suggest you to read the following page: http://www.fltk.org/doc-1.3/basics.html . It clearly explains how to compile a simple FLTK program.
Assuming your FLTK application is not using extra FLTK libraries (opengl, images, forms) and is in a single file, called myfltkapp.cpp, the absolutely quickest way to compile and link it properly would be:
fltk-config --compile myfltkapp.cpp
I guess the reason I got the error comes from the difference between gcc and g++. I thought I could compile it by gcc with fltk library, but gcc does not link c++ standard library automatically. Thus you have to include the C++ standard library for gcc. It was a silly question.
gcc myfltkapp.cpp -lstdc++ -lfltk
What is the difference between g++ and gcc?

Static cross-compilation of GTK applications on Fedora for Windows?

I'm trying to build gtk3 applications for windows and since Fedora delivers mingw and precompiled libraries for gtk3 for mingw, I used it.
I got the normal compilation working via
i686-mingw32-gcc test.c `pkg-config --cflags --libs gtk+-win32-3.0`
Now I would like to link it statically (Fedora delivers precompiled libraries for that too) but i cannot get it to work. It tryed
i686-mingw32-gcc test.c -static `pkg-config --cflags --libs --static gtk+-win32-3.0`
but it results in
/usr/lib/gcc/i686-pc-mingw32/4.6.1/../../../../i686-pc-mingw32/bin/ld: cannot find -lgtk-3
/usr/lib/gcc/i686-pc-mingw32/4.6.1/../../../../i686-pc-mingw32/bin/ld: cannot find -lgdk-3
/usr/lib/gcc/i686-pc-mingw32/4.6.1/../../../../i686-pc-mingw32/bin/ld: cannot find -lgdk_pixbuf-2.0
/usr/lib/gcc/i686-pc-mingw32/4.6.1/../../../../i686-pc-mingw32/bin/ld: cannot find -lpng14
/usr/lib/gcc/i686-pc-mingw32/4.6.1/../../../../i686-pc-mingw32/bin/ld: cannot find -lffi
AFAIK, static compilation is not supported for GTK+. Anyway, gcc arguments order is important for building with mingw.

Makefile for a simple application

I have a file app.c which uses two libraries GStreamer and libXml2. To compile the application I type the following on Terminal
gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) app.c -o app -I/usr/include/libxml2 -lxml2
When I try to Makefile with the contents as follows :
all:
gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) app.c -o app -I/usr/include/libxml2 -lxml2
run:
./app
clean:
rm app
On running make command I get the errors as expected. What is the significance of
$(pkg-config --cflags --libs gstreamer-0.10)
on Echoing the above I get some files which when included in Makefile gives me the correct output.
pkg-config --cflags libraryX outputs the path to the header files of libraryX. Without this, the compiler does not know where to look for the header files, and compilation will fail.
Similarly, pkg-config --libs libraryX outputs the path to the actual compiled library files of libraryX. Without this, the linker does not know where to look for the library files, and linking will fail.
pkg-config --cflags --libs libraryX is just combining what I described above. Since you're using gcc to do both compilation and linking, you just pass those parameters together to gcc.

Resources