有很多类似的问题,但不幸的是他们没有太大帮助。
我正在尝试从 this 构建程序发帖,报错:
$ 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
有include <dbus/dbus.h>
, 以及系统上存在的文件:
# find / -name "dbus.h" -type f
/usr/include/dbus-1.0/dbus/dbus.h
但是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);
我不是 C 开发人员,对 gcc
不是很熟悉和链接器,所以任何提示/链接表示赞赏。
您的链接顺序是从后到前。而不是:
gcc `pkg-config --libs --cflags dbus-1` hh.c -o hh
做:
gcc hh.c -o hh `pkg-config --libs --cflags dbus-1`
或:
gcc hh.c `pkg-config --libs --cflags dbus-1` -o hh
在链接序列中,需要符号定义的文件必须在提供定义的文件之前。所以库在目标文件之后。如果不清楚这如何适用于您的命令行读取 this question和我的答案。
我是一名优秀的程序员,十分优秀!