有很多类似的问题,但不幸的是,它们并没有多大帮助。
我正在尝试从这篇文章构建程序,但出现错误:
$ 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
Run Code Online (Sandbox Code Playgroud)
系统上存在include <dbus/dbus.h>, 和文件:
# find / -name "dbus.h" -type f
/usr/include/dbus-1.0/dbus/dbus.h
Run Code Online (Sandbox Code Playgroud)
但dbus_error_init例如存在于dbus-errors.h文件中:
# 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);
Run Code Online (Sandbox Code Playgroud)
我不是 C 开发人员,也不太熟悉gcc和链接器,因此感谢任何提示/链接。
您的链接顺序是从后到前的。代替:
gcc `pkg-config --libs --cflags dbus-1` hh.c -o hh
Run Code Online (Sandbox Code Playgroud)
做:
gcc hh.c -o hh `pkg-config --libs --cflags dbus-1`
Run Code Online (Sandbox Code Playgroud)
或者:
gcc hh.c `pkg-config --libs --cflags dbus-1` -o hh
Run Code Online (Sandbox Code Playgroud)
在链接序列中,需要符号定义的文件必须位于提供定义的文件之前。所以库在目标文件之后。如果不清楚这如何适用于您的命令行,请阅读此问题和我的回答。