如何编译基本的D-Bus / glib示例?

我正在尝试学习如何使用带有C绑定的D-Bus。 我以前从未使用过D-Bus。 我正在学习本教程 ,我认为这是官方教程 (Freedesktop.org)。 我已经读过它,直到这个段落给出了第一个示例程序,但不幸的是我在这个页面上没有看到任何关于如何编译它或包含哪些库的指示。 我错过了什么 ?

我的操作系统是Ubuntu 10.04 32bit。 我安装了libdbus-glib-1-dev软件包。 我尝试在源文件的开头添加#include ,并使用

 $ gcc -ldbus-1 -I/usr/include/dbus-1.0/ -I/usr/lib/i386-linux-gnu/dbus-1.0/include -o my_dbus.bin my_dbus.c 

但我一直都在失败:

 my_dbus.c: In function 'main': my_dbus.c:7:3: error: unknown type name 'DBusGConnection' my_dbus.c:8:3: error: unknown type name 'GError' ... 

我是否错过了教程中的一点? 不,你能帮我编译一下这段代码吗?

提前致谢。

像这样的教程通常假设您已经了解了它所编写的语言,在本例中为C,以及您将在其上运行的操作系统。

看一下教程,我看到它只包含一个main函数。 因此,您需要添加适当的#include指令才能使其正常工作:

 #include  // for exit() #include  // for dbus_* #include  // for dbus_g_* 

此外,您需要编译库(在本例中为dbusdbus-glib ),或者使用操作系统中预编译的库,以便将它们链接到可执行文件。

您还需要随源提供的头文件或操作系统中的“开发”软件包。

例如,在我的Ubuntu工作站上,我可以安装源文件和头文件,如下所示:

 sudo apt-get -y install dbus libdbus-1-dev libdbus-glib-1-2 libdbus-glib-1-dev 

编译(或正确安装)后,继续编译程序。 您需要指定正确的包含路径和库以链接到编译器/链接器。 例如,使用GCC和我目前的设置,它将是:

 gcc test.c -I/usr/include/dbus-1.0 \ -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include \ -I/usr/include/glib-2.0 \ -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ \ -ldbus-1 \ -ldbus-glib-1 -Wall -Wextra 

这应该在当前目录中创建一个执行a.out

当然,我有几年的C和Linux经验,所以我很容易弄清楚这些东西。 如果你想从C开始,你可能应该从更简单的开始。

基于‘netcoder’的答案是对我有用的程序。

 #include  // for exit() #include  // for dbus_* #include  // for dbus_g_* int main (int argc, char **argv) { DBusGConnection *connection; GError *error; DBusGProxy *proxy; char **name_list; char **name_list_ptr; g_type_init (); error = NULL; connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (connection == NULL) { g_printerr ("Failed to open connection to bus: %s\n", error->message); g_error_free (error); exit (1); } /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */ proxy = dbus_g_proxy_new_for_name (connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); /* Call ListNames method, wait for reply */ error = NULL; if (!dbus_g_proxy_call (proxy, "ListNames", &error, G_TYPE_INVALID, G_TYPE_STRV, &name_list, G_TYPE_INVALID)) { /* Just do demonstrate remote exceptions versus regular GError */ if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) g_printerr ("Caught remote method exception %s: %s", dbus_g_error_get_name (error), error->message); else g_printerr ("Error: %s\n", error->message); g_error_free (error); exit (1); } /* Print the results */ g_print ("Names on the message bus:\n"); for (name_list_ptr = name_list; *name_list_ptr; name_list_ptr++) { g_print (" %s\n", *name_list_ptr); } g_strfreev (name_list); g_object_unref (proxy); return 0; } 

和Makefile

 file=1 sample: g++ -g $(file).cc -o $(file) -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ -ldbus-1 -ldbus-glib-1 -Wall -Wextra -lglib-2.0 -lgio-2.0 -lgobject-2.0 -lgthread-2.0 

注意:此网页有一个很好的D-bus示例https://developer.gnome.org/gio//2.36/GDBusProxy.html

看起来你必须单独包含 ,因为不会自动包含它

请注意,libdbus-glib已弃用,未维护,不应用于从C访问D-Bus:请改用GDBus 。 也不建议使用libdbus-1:它是维护的,但是使用D-Bus的API级别低得多,并且没有GDBus的所有便利function。

正如热心人所说,有很好的GDBus文档可用 。

(libdbus-glib和libdbus-1故意没有关联,以避免给他们google汁液。)

基于gcc命令返回的错误。 gcc能够看到文件(否则它将显示错误消息,表明他无法看到头文件),但无法看到该文件中应存在的一些变量( 'DBusGConnection''GError' )。 可能是你没有使用适当版本的dbus

并尝试使用make文件而不是一个命令

 LDFLAGS+=-ldbus CFLAGS+=-I/usr/include/dbus-1.0/ CFLAGS+=-I/usr/lib/i386-linux-gnu/dbus-1.0/include all: dbus-example.bin %.o: %.c $(CC) $(CFLAGS) -c -o $@ $^ dbus-example.bin: my_dbus.o $(CC) $(LDFLAGS) -o $@ $^ clean: rm -f *.o dbus-example.bin