使用 dbus 头文件编译 C 程序
我正在尝试编译一个 C 程序,其中包含以下标头: http://pastebin.com/SppCXb0U ,乌班图。起初我一点运气都没有,但在阅读了有关 pkg-config 的内容后,我制作了这一行:
gcc `pkg-config --cflags --libs dbus-1` `pkg-config --cflags --libs glib-2.0` signals-tutorial.c
然而,它仍然不起作用并给了我这个错误:
/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
我不知道从这里做什么。
====================================
一个很好的解释 - 谢谢。但是,我无法让它工作。运行上面的命令(添加了 )会产生以下结果
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题不在于头文件,你的问题在于库;关于“未定义引用”的抱怨通常来自链接器。您需要将库配置选项放在源文件之后:
--libs
选项将为编译器生成一系列-l
标志,编译器会将它们传递给链接器。链接器将从左到右从目标文件(或者在本例中足够接近的 C 源文件)开始解析符号,因此所有库-l
开关都需要遵循您的源文件。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:
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.