2

I have a simple program

#include <glib.h>
int main(){
  g_print("hallo\n");
}

and try to compile it on the embedded system (Odroid X2) with Ubuntu like that

root@odroid:~/# gcc $(pkg-config --libs --cflags glib-2.0) -o main main.c
/tmp/cci48ASK.o: In function `main':
main.c:(.text+0xc): undefined reference to `g_print'
collect2: error: ld returned 1 exit status

Installed compiler:

root@odroid:~/x# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.7/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.3-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libitm --enable-plugin --with-system-zlib --enable-objc-gc --with-cloog --enable-cloog-backend=ppl --disable-cloog-version-check --disable-ppl-version-check --enable-multiarch --enable-multilib --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --disable-werror --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix

Any ideas why the linker does not find the reference?

gschaden
  • 655
  • 7
  • 14
  • 1
    Your command worked exactly as-is for me on Fedora 18. I have `gcc` version 4.7.2. What is your version (your `gcc -v` output was cut off before the last line)? – lurker Sep 18 '13 at 16:34

3 Answers3

4

The order of options to gcc (and other compilers) matters.

gcc -Wall main.c $(pkg-config --libs --cflags glib-2.0) -o main

And I don't really like the above. You should learn how to use GNU make. At the very least, put the compile flags, then the sources, then the object files, then the libraries (from high level to low level).

gcc -Wall $(pkg-config  --cflags glib-2.0) main.c \
    $(pkg-config --libs glib-2.0) -o main

Better yet, have a Makefile starting with

CC=gcc
CFLAGS= -Wall  $(pkg-config  --cflags glib-2.0) 
LIBES= $(pkg-config --libs glib-2.0)

And compiling as root should be avoided. Only installation should require root privilege...

You probably want to add -g (compiler flag for debugging information). Once the program is ready and nearly bugfree, replace it with -O2 (optimizations) and do some serious testing again!

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Ok found a solution

gcc -o main main.c  `pkg-config --libs --cflags glib-2.0`

works, but I don't know why on my X64 linux system it worked also the other way.

gschaden
  • 655
  • 7
  • 14
0

You need to add the $(pgk-config --libs glib-2.0) after the main.c in your compile line - because library functions are only dragged into the binary if there is anything using them - so main.c is what is using the g_print, and if the -lglib (or whatever the result of the pkg-config bit is) is before main.c, it doesn't get included in the resulting binary.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227