Search code examples
cgccglib

gcc glib symbol lookup error


I am currently programming a little C-programm that uses GLib-2.0 for its data structures.

I am compiling with the following command:

gcc -g -O3 -std=gnu99 -fPIC -shared -I/usr/local/java/jdk1.8.0_20/include \
    -I/usr/local/java/jdk1.8.0_20/include/linux \
    `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` \
    -o runtimesamplingagent.so runtimesamplingagent.c

This works without problems.

However, when I run the program, I get following error:

 symbol lookup error: ./runtimesamplingagent.so: undefined symbol: g_node_new

What's the cause of this?

I am pretty new to C but I would guess it's some kind of linking issue. Any hints what could be the cause of it?

I am working on a Ubuntu 14.04 LTS 64bit machine with libglib-2.0-dev installed.


Solution

  • You have already included the linking option in the build command; the problem is that it is in the wrong place.

    Your command looks like this:

    gcc -g -O3 -std=gnu99 -fPIC -shared \
      -I/usr/local/java/jdk1.8.0_20/include \
      -I/usr/local/java/jdk1.8.0_20/include/linux \
      `pkg-config --cflags glib-2.0` \
      `pkg-config --libs glib-2.0` \       <<<
      -o runtimesamplingagent.so \
      runtimesamplingagent.c
    

    In that line marked with <<<, you have already specified the options needed to link GLib. The only problem is that it is in the wrong place. You should put this at the very end, after the .c file.

    So your command should look like this instead:

    gcc -g -O3 -std=gnu99 -fPIC -shared \
      -I/usr/local/java/jdk1.8.0_20/include \
      -I/usr/local/java/jdk1.8.0_20/include/linux \
      `pkg-config --cflags glib-2.0` \
      -o runtimesamplingagent.so \
      runtimesamplingagent.c \
      `pkg-config --libs glib-2.0`