gpt4 book ai didi

c++ - 无法将 dbus 与 C++ 链接

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:18 26 4
gpt4 key购买 nike

我有一个简单的程序,其中包括 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++

...
You can mix options and other arguments. For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.
...
-llibrary
...
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
...

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com