1

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.

setevoy
  • 4,374
  • 11
  • 50
  • 87
  • If you run `pkg-config --libs --cflags dbus-1` alone, what's the output? – fluter May 10 '16 at 08:54
  • `$ pkg-config --libs --cflags dbus-1 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -ldbus-1` – setevoy May 10 '16 at 08:55
  • You need to link your application to the dbus-1 library. – Jan Henke May 10 '16 at 09:06
  • @JanHenke Thanks. Could you add this as an Answer? – setevoy May 10 '16 at 09:16
  • That cannot be the answer (I removed my answer which said the same). Look at what `pkg-config` is producing. Perhaps the `pkg-config` is misplaced in the command line and needs to be at the end or something? – trojanfoe May 10 '16 at 09:58

3 Answers3

5

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.

Community
  • 1
  • 1
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
1

The missing symbols are defined in the dbus-1 library. You have to tell gcc to link to that library. If you have the library installed in a default location on your system, the -ldbus-1 flag should be enough to tell the linker to use that library in resolving the missing symbols.

Jan Henke
  • 875
  • 1
  • 15
  • 28
  • But it apparently does not work with ```pkg-config```, since the undefined symbols mentioned by the OP are clearly defined by libdbus-1.so.3 on my system. – Jan Henke May 10 '16 at 10:53
  • Understood; but you can see (from the comment) that `-ldbus-1` is already being emitted by `pkg-config`. – trojanfoe May 10 '16 at 10:54
0

I tried the example, and it built for me.

First of all, you're getting a linking error and not a compilation error so there's a problem finding your library.

Here's my library:

/usr/lib/x86_64-linux-gnu/libdbus-1.so

I'm running Devuan GNU/Linux 1.0 (jessie) which is similar Debian/Ubuntu.

  1. How did you install your dbus library?

I got mine like this:

$ sudo apt-get install --reinstall libdbus-1-dev