我有一个简单的程序,其中包括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选项放在最后:

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++

关于c++ - 无法将dbus与C++链接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24884679/

10-10 12:34