简体   繁体   English

无法将dbus与C ++链接

[英]Can not link dbus with c++

I have simple program which includes dbus and uses basic functions eg: 我有一个简单的程序,其中包括dbus并使用基本功能,例如:

 DBusError err;
 dbus_error_init(&err);

When I try to compile the program 当我尝试编译程序时

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

I get following errors: 我收到以下错误:

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

I do not understand it. 我不明白。 I tried to compile similiar application but written in C and everything compiled and linked. 我尝试编译类似的应用程序,但是用C编写,所有内容都进行了编译和链接。

Just put -l options at the end: 只需将-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

Here is related question: What is the proper sequence of options for gcc & the importance of that sequence? 这是一个相关的问题: gcc选项的正确顺序是什么以及该顺序的重要性?

This behaviour is expected and according to documentations: 此行为是预期的,并且根据文档:

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. 例如,如果您多次指定-L,则按指定的顺序搜索目录。 Also, the placement of the -l option is significant. 同样,-l选项的位置也很重要。
... ...
-llibrary -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. 因此,foo.o -lz bar.o在文件foo.o之后但在bar.o之前搜索库z。 If bar.o refers to functions in z, those functions may not be loaded. 如果bar.o引用z中的函数,则可能不会加载这些函数。
... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM