如何正确编译和 link gdbus 程序

How to properly compile and link a gdbus program

我正在尝试学习 Linux 上的 dbus 和 compilation/linking 程序。我对从头开始构建和链接应用程序还很陌生。为此,我在 Ubuntu 上创建了一个简单的客户端+服务器应用程序,它通过 gdbus 进行通信。我正在使用 gdbus-codegen 工具为 dbus 接口生成 .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.

为什么我的系统上没有这个头文件?我在 /usr/include/glib-2.0/gio 中有 gio 目录,它包含一堆头文件 - 但不包含 gunixfdlist.h.

旁注:

  1. 我不是故意在构建命令中使用 pkg-config 以便更好地理解 pkg-config 在编译过程中扩展到什么
  2. 我想我必须向链接器和我的构建命令提供实际的库位置,但我想先解决没有正确解析包含的问题

头文件在/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 the gio-unix-2.0.pc pkg-config file when using it.

因此您应该在项目配置中使用 pkg-config,并确保 gio-unix-2.0 包含在获取编译标志的调用中。