Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc undefined reference to dbus_*

Tags:

c

gcc

There is a lot of similar questions, but unfortunately they didn't help a lot.

I'm trying to build program from this post, and have an error:

$ gcc `pkg-config --libs --cflags dbus-1` hh.c -o hh
/tmp/ccMabXOg.o: In function `main':
hh.c:(.text+0x18): undefined reference to `dbus_error_init'
hh.c:(.text+0x29): undefined reference to `dbus_bus_get'
hh.c:(.text+0x39): undefined reference to `dbus_error_is_set'
hh.c:(.text+0x5f): undefined reference to `dbus_error_free'
hh.c:(.text+0x80): undefined reference to `dbus_bus_name_has_owner'
hh.c:(.text+0x8f): undefined reference to `dbus_error_is_set'
hh.c:(.text+0x9f): undefined reference to `dbus_error_free'
hh.c:(.text+0xfb): undefined reference to `dbus_bus_request_name'
hh.c:(.text+0x10a): undefined reference to `dbus_error_is_set'
hh.c:(.text+0x11a): undefined reference to `dbus_error_free'
collect2: error: ld returned 1 exit status

There is include <dbus/dbus.h>, and file present on the system:

# find / -name "dbus.h" -type f
/usr/include/dbus-1.0/dbus/dbus.h

But dbus_error_init for example present in dbus-errors.h file:

# grep -r dbus_error_init /usr/include/dbus-1.0/dbus/
/usr/include/dbus-1.0/dbus/dbus-errors.h:void        dbus_error_init      (DBusError       *error);

I'm not C developer and not too much familiar with gcc and the linker, so any tips/links appreciated.

like image 669
setevoy Avatar asked May 10 '26 12:05

setevoy


1 Answers

Your linkage order is back-to-front. Instead of:

gcc `pkg-config --libs --cflags dbus-1` hh.c -o hh

do:

gcc hh.c -o hh `pkg-config --libs --cflags dbus-1`

or:

gcc hh.c `pkg-config --libs --cflags dbus-1` -o hh

In the linkage sequence, files that need symbol definitions must come before the ones that provide the definitions. So libraries come after object files. If it is unclear how this applies to your commandline read this question and my answer.

like image 119
Mike Kinghan Avatar answered May 12 '26 05:05

Mike Kinghan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!