我正在尝试学习 Linux 上的 dbus 和程序编译/链接。我对从头开始构建和链接应用程序相当陌生。为此,我在 Ubuntu 上创建一个简单的客户端+服务器应用程序,通过 gdbus 进行通信。我正在使用 gdbus-codegen 工具为 dbus 接口(interface)生成 .c 和 .h 文件。我创建了一个名为 dbus_interface.xml 的示例 xml 描述文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE node PUBLIC
"-//freedesktop//DTD D-Bus Object Introspection 1.0//EN"
"http://standards.freedesktop.org/dbus/1.0/introspect.dtd">
<node>
<interface name="org.hello.world">
<method name="get_magic_number">
<arg type="i" name="magic_number" direction="out"/>
</method>
</interface>
</node>
我使用以下命令生成代码:
gdbus-codegen --generate-c-code generated_code dbus_interface.xml
生成 generated_code.c 和 generated_code.h 文件。我已将 generated_code.h 头文件包含在我的客户端应用程序中,我尝试使用以下命令使用 gcc 对其进行编译:
gcc -Wall -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include client.c generated_code.c -o client
但是,我收到以下错误:
generated_code.c:17:12: fatal error: gio/gunixfdlist.h: No such file or directory
17 | # include <gio/gunixfdlist.h>
| ^~~~~~~~~~~~~~~~~~~
compilation terminated.
为什么我的系统上不存在这个头文件?我的 gio 目录位于/usr/include/glib-2.0/gio 中,它包含一堆头文件 - 但不包含gunixfdlist.h。
附注:
- 我没有故意在构建命令中使用 pkg-config,以便更好地理解 pkg-config 在编译过程中扩展为什么
- 我想我必须向链接器以及我的构建命令提供实际的库位置,但我想首先解决无法正确解析包含的问题
最佳答案
头文件位于 /usr/include/gio-unix-2.0/gio
。如 documentation 中所述:
Note that
<gio/gunixfdlist.h>
belongs to the UNIX-specific GIO interfaces, thus you have to use thegio-unix-2.0.pc
pkg-config file when using it.
因此,您应该在项目配置中使用 pkg-config,并确保 gio-unix-2.0
包含在获取编译标志的调用中。
关于c - 如何正确编译和链接 gdbus 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63692782/