Poisson
Poisson

Reputation: 1623

Glib linking error: undefined reference to

I am not sure if this is the correct way of asking my question.. I just tried to solve the problem for 4 days, and I am extremely frustrated right now... I have the following issue:

I installed Ubuntu 13. and some libraries and packages afterwards. I then installed glib2 Everything went fine, I got no errors when doing ./configure, make, or make install. However, when I try to compile a test file in C, I can compile without errors, but I get the following errors:

main.c:116: référence indéfinie vers « g_hash_table_new_full »

main.c:139: référence indéfinie vers « g_hash_table_lookup »

main.c:168: référence indéfinie vers « g_hash_table_insert »

main.c:181: référence indéfinie vers « g_hash_table_lookup »

main.c:211: référence indéfinie vers « g_hash_table_insert »

main.c:250: référence indéfinie vers « g_hash_table_lookup »

I compile with:

gcc -D_GNU_SOURCE -I/usr/include/glib-2.0 -I/usr/include/glib-2.0/glib.h -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ -lglib-2.0 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -g -Wall -Wextra -std=c99 -lm *.c -o formattage

pkg-config --cflags --libs glib-2.0 gives me:

-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0

I try with this commande :

gcc -v -H -std=c99 -g -Wall -D_GNU_SOURCE $(pkg-config --cflags glib-2.0) *.c $(pkg-config --libs glib-2.0) -o formattage

But i got this message :

gcc -v -H -std=c99 -g -Wall -D_GNU_SOURCE *.c -o formattage Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9) COLLECT_GCC_OPTIONS='-v' '-H' '-std=c99' '-g' '-Wall' '-D' '_GNU_SOURCE' '-o' 'formattage' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-linux-gnu/4.8/cc1 -quiet -v -imultiarch x86_64-linux-gnu -H -D _GNU_SOURCE main.c -quiet -dumpbase main.c -mtune=generic -march=x86-64 -auxbase main -g -Wall -std=c99 -version -fstack-protector -Wformat -Wformat-security -o /tmp/ccuxfazT.s GNU C (Ubuntu/Linaro 4.8.1-10ubuntu9) version 4.8.1 (x86_64-linux-gnu) compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.1-p2, MPC version 1.0.1 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../x86_64-linux-gnu/include"

#include "..." search starts here:
 #include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C (Ubuntu/Linaro 4.8.1-10ubuntu9) version 4.8.1 (x86_64-linux-gnu)
    compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.1-p2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: b946adc090b5605f865521be8b22959c
. /usr/include/stdlib.h
.. /usr/include/features.h
... /usr/include/x86_64-linux-gnu/sys/cdefs.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
... /usr/include/x86_64-linux-gnu/gnu/stubs.h
.... /usr/include/x86_64-linux-gnu/gnu/stubs-64.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
.. /usr/include/x86_64-linux-gnu/bits/waitflags.h
.. /usr/include/x86_64-linux-gnu/bits/waitstatus.h
... /usr/include/endian.h
.... /usr/include/x86_64-linux-gnu/bits/endian.h
.... /usr/include/x86_64-linux-gnu/bits/byteswap.h
..... /usr/include/x86_64-linux-gnu/bits/types.h
...... /usr/include/x86_64-linux-gnu/bits/wordsize.h
...... /usr/include/x86_64-linux-gnu/bits/typesizes.h
..... /usr/include/x86_64-linux-gnu/bits/wordsize.h
..... /usr/include/x86_64-linux-gnu/bits/byteswap-16.h
.. /usr/include/xlocale.h
.. /usr/include/x86_64-linux-gnu/sys/types.h
... /usr/include/time.h
... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
... /usr/include/x86_64-linux-gnu/sys/select.h
.... /usr/include/x86_64-linux-gnu/bits/select.h
..... /usr/include/x86_64-linux-gnu/bits/wordsize.h
.... /usr/include/x86_64-linux-gnu/bits/sigset.h
.... /usr/include/time.h
.... /usr/include/x86_64-linux-gnu/bits/time.h
... /usr/include/x86_64-linux-gnu/sys/sysmacros.h
... /usr/include/x86_64-linux-gnu/bits/pthreadtypes.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
.. /usr/include/alloca.h
... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
.. /usr/include/x86_64-linux-gnu/bits/stdlib-float.h
. /usr/include/stdio.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
.. /usr/include/libio.h
... /usr/include/_G_config.h
.... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
.... /usr/include/wchar.h
... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stdarg.h
.. /usr/include/x86_64-linux-gnu/bits/stdio_lim.h
.. /usr/include/x86_64-linux-gnu/bits/sys_errlist.h
. /usr/include/string.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
main.c:5:18: fatal error: glib.h: No such file or directory
 #include <glib.h>
                  ^
compilation terminated.
make: *** [all] Error 1

I think it become more complex

Thanks,

Upvotes: 0

Views: 8611

Answers (3)

Order of program arguments to gcc matters a lot (source files should always go before linked libraries).

Compile with

 gcc -std=c99 -g -Wall -D_GNU_SOURCE $(pkg-config --cflags glib-2.0) \
       *.c  \
       $(pkg-config --libs glib-2.0)  -o formattage

But you really want to use a Makefile similar to this one

Of course, you also need to install the libglib2.0-dev package, as reminded by this answer.

BTW: I am (as you probably are) a native French speaker, but on S.O. you should ask questions in English and get error messages in English. Set your LANG environment variable to C with export LANG=C before compiling for Stack Overflow.

Upvotes: 4

yokto
yokto

Reputation: 753

Since the there is no error about not finding -lglib-2.0 we can assume that this was found and passed to the linker. However the symbols are still missing.

I don't know how this could happen. You could check if the file contains the symbols

readelf -Ws /usr/lib/x86_64-linux-gnu/libglib-2.0.so | grep g_hash_table

Otherwise, did I understand right that you installed glib2 from source? Is there a reason you didn't install it using

sudo apt-get install libglib2.0-dev

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249093

You need to "link" against the library which implements the interfaces you have included with those -I options. That means adding something like -lglib2.0 to your build command. If the library is not found due to be in a nonstandard location, you will first need to say -L/some/path/to/glib with the location of libglib2.0.

Upvotes: 0

Related Questions