7

I'm trying to compile a C program, with these headers: http://pastebin.com/SppCXb0U , on Ubuntu. At first I didn't have any luck at all, but after reading about pkg-config I crafted this line:

gcc `pkg-config --cflags --libs dbus-1` `pkg-config --cflags --libs glib-2.0` signals-tutorial.c

However, it still doesn't work and gives me this error:

/tmp/cc3BkbdA.o: In function `filter_example':
signals-tutorial.c:(.text+0x1a3): undefined reference to `dbus_connection_setup_with_g_main'
/tmp/cc3BkbdA.o: In function `proxy_example':
signals-tutorial.c:(.text+0x29a): undefined reference to `g_type_init'
signals-tutorial.c:(.text+0x2b3): undefined reference to `dbus_g_bus_get'
signals-tutorial.c:(.text+0x323): undefined reference to `dbus_g_proxy_new_for_name'
signals-tutorial.c:(.text+0x369): undefined reference to `dbus_g_proxy_add_signal'
signals-tutorial.c:(.text+0x38a): undefined reference to `dbus_g_proxy_connect_signal'
collect2: ld returned 1 exit status

I'm not sure what to do from here.

==================================

A nice explanation - thank you. However, I can't get it working. Running your command above (with an added ) yield the following result

gcc `pkg-config --cflags dbus-1` \
>     `pkg-config --cflags glib-2.0` \
>     signals-tutorial.c \
>     `pkg-config --libs dbus-1` \
>     `pkg-config --libs glib-2.0`
/tmp/ccjN0QMh.o: In function `filter_example':
signals-tutorial.c:(.text+0x1a3): undefined reference to `dbus_connection_setup_with_g_main'
/tmp/ccjN0QMh.o: In function `proxy_example':
signals-tutorial.c:(.text+0x29a): undefined reference to `g_type_init'
signals-tutorial.c:(.text+0x2b3): undefined reference to `dbus_g_bus_get'
signals-tutorial.c:(.text+0x323): undefined reference to `dbus_g_proxy_new_for_name'
signals-tutorial.c:(.text+0x369): undefined reference to `dbus_g_proxy_add_signal'
signals-tutorial.c:(.text+0x38a): undefined reference to `dbus_g_proxy_connect_signal'
collect2: ld returned 1 exit status
Martin Hansen
  • 571
  • 4
  • 8
  • 14

1 Answers1

13

Your problem isn't with the header files, your problem is with the libraries; complaints about "undefined references" generally come from the linker. You need to put the library configuration options after the source file:

gcc `pkg-config --cflags dbus-glib-1` \
    `pkg-config --cflags dbus-1` \
    `pkg-config --cflags glib-2.0` \
    signals-tutorial.c \
    `pkg-config --libs dbus-glib-1` \
    `pkg-config --libs dbus-1` \
    `pkg-config --libs glib-2.0`

The --libs option will produce a series of -l flags for the compiler, the compiler will pass those to the linker. The linker will resolve symbols from left to right starting with the object file (or, close enough in this case, the C source file) so all the library -l switches need to follow your source file.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Thank you very much for your nice answer. The comment field isn't large enough to post my results, so I pasted it in the main post. – Martin Hansen Apr 22 '11 at 01:26
  • What do the `pkg-config --libs dbus-1` and `pkg-config --libs glib-2.0` output? – mu is too short Apr 22 '11 at 01:32
  • `-L/usr/lib/x86_64-linux-gnu -ldbus-1 -lpthread -lrt` and `-L/usr/lib/x86_64-linux-gnu -lglib-2.0` respectivly – Martin Hansen Apr 22 '11 at 01:35
  • 3
    And what happens if you add `pkg-config --cflags dbus-glib-1` before your C file and `pkg-config --libs dbus-glib-1` after? They should probably go before dbus-1 in both places but you might have to play with the order a bit. I think you might need more than just the dbus-1 and glib-2.0 libraries. – mu is too short Apr 22 '11 at 01:40
  • @Martin: I updated the command with the final `dbus-glib-1` dependency for posterity. – mu is too short Apr 22 '11 at 19:37