Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C program with dbus header files

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
like image 720
Martin Hansen Avatar asked Apr 22 '11 00:04

Martin Hansen


1 Answers

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.

like image 69
mu is too short Avatar answered Sep 22 '22 11:09

mu is too short