加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用dbus头文件编译C程序

发布时间:2020-12-16 03:08:12 所属栏目:百科 来源:网络整理
导读:我正在尝试使用这些头文件编译一个C程序: http://pastebin.com/SppCXb0U,在Ubuntu上.起初我根本没有任何运气,但在阅读了pkg-config之后我制作了这一行: gcc `pkg-config --cflags --libs dbus-1` `pkg-config --cflags --libs glib-2.0` signals-tutorial.
我正在尝试使用这些头文件编译一个C程序: http://pastebin.com/SppCXb0U,在Ubuntu上.起初我根本没有任何运气,但在阅读了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

解决方法

您的问题不在于头文件,您的问题在于库;关于“未定义的引用”的抱怨通常来自链接器.您需要将库配置选项放在源文件之后:
gcc `pkg-config --cflags dbus-glib-1` 
    `pkg-config --cflags dbus-1` 
    `pkg-config --cflags glib-2.0` 
    signals-tutorial.c 
    `pkg-config --libs dbus-glib-1` 
    `pkg-config --libs dbus-1` 
    `pkg-config --libs glib-2.0`

–libs选项将为编译器生成一系列-l标志,编译器会将这些标志传递给链接器.链接器将从目标文件开始从左到右解析符号(或者,在这种情况下,C源文件足够接近),因此所有库-l开关都需要遵循源文件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读