【问题标题】:Can not link dbus with c++无法将 dbus 与 C++ 链接
【发布时间】:2014-07-22 10:18:23
【问题描述】:

我有一个简单的程序,其中包括 dbus 并使用基本功能,例如:

 DBusError err;
 dbus_error_init(&err);

当我尝试编译程序时

g++ -Wall --std=c++11 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include  -ldbus-1 main.cpp

我收到以下错误:

main.cpp:(.text+0x4b): undefined reference to `dbus_error_init'
main.cpp:(.text+0x5a): undefined reference to `dbus_bus_get_private'
main.cpp:(.text+0x79): undefined reference to `dbus_error_free'
main.cpp:(.text+0xf4): undefined reference to `dbus_connection_set_exit_on_disconnect'
main.cpp:(.text+0x114): undefined reference to `dbus_bus_request_name'
main.cpp:(.text+0x123): undefined reference to `dbus_error_is_set'
main.cpp:(.text+0x138): undefined reference to `dbus_error_free'
collect2: error: ld returned 1 exit status

我不明白。我尝试编译类似的应用程序,但用 C 语言编写,所有内容都编译和链接。

【问题讨论】:

  • 我认为您需要添加 -L 选项来指示库
  • @Uday 不,它不起作用
  • @Uday, -L 选项表示库的路径。

标签: c++ linux linker g++ dbus


【解决方案1】:

只需将-l 选项放在末尾即可:

g++ -Wall --std=c++11 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include main.cpp -ldbus-1

这里是相关问题:What is the proper sequence of options for gcc & the importance of that sequence?

这种行为是预期的,根据文档:

man g++

...
您可以混合使用选项和其他参数。在大多数情况下,您使用的顺序无关紧要。使用时顺序很重要 多个同类选项;例如,如果您指定 -L 更多 不止一次,目录按指定的顺序搜索。 此外,-l 选项的位置也很重要。
...
-图书馆
...
在命令中编写此选项会有所不同;这 链接器按顺序搜索和处理库和目标文件 它们是指定的。因此,foo.o -lz bar.o 在文件 foo.o 之后但在 bar.o 之前搜索库 z。如果 bar.o 引用 z 中的函数,则可能不会加载这些函数。
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-01
    • 2015-09-21
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多