C语言 如何编译一个基本的 D-Bus/glib 示例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14263390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile a basic D-Bus/glib example?
提问by tvuillemin
I'm trying to learn how to use D-Bus with C bindings. I've never used D-Bus before. I'm following this tutorial, which I assume is the official one (Freedesktop.org). I've read it until this paragraphthat gives a first sample program , but unfortunately I don't see any indication on this page about how to compile it or which libraries to include. Did I miss something ?
我正在尝试学习如何将 D-Bus 与 C 绑定一起使用。我以前从未使用过 D-Bus。我正在关注本教程,我认为它是官方教程(Freedesktop.org)。我已经阅读了它,直到本段给出了第一个示例程序,但不幸的是,我在此页面上没有看到有关如何编译它或包含哪些库的任何指示。我错过了什么 ?
My OS is Ubuntu 10.04 32bit. I installed the libdbus-glib-1-devpackage. I tried to add #include <dbus/dbus.h>at the beginning of the source file, and to compile with
我的操作系统是 Ubuntu 10.04 32 位。我安装了这个libdbus-glib-1-dev包。我试图#include <dbus/dbus.h>在源文件的开头添加,并编译
$ 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
but I just keep failing:
但我一直失败:
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'
...
Did I miss a point in the tutorial ? It not, could you please help me compile this piece of code ?
我错过了教程中的一点吗?不是,你能帮我编译这段代码吗?
Thanks in advance.
提前致谢。
回答by netcoder
Tutorials like this one generally assume that you have some knowledge of the language it is written for, in this case C, as well as the operating system you will run it on.
像这样的教程通常假设您对它所用的语言(在本例中为 C)以及将在其上运行的操作系统有一定的了解。
Looking at the tutorial, I see that it only contains a mainfunction. As such, you will need to add the proper #includedirectives in order for this to work:
查看教程,我看到它只包含一个main函数。因此,您需要添加适当的#include指令才能使其正常工作:
#include <stdlib.h> // for exit()
#include <dbus/dbus.h> // for dbus_*
#include <dbus/dbus-glib.h> // for dbus_g_*
Also, you will need to compile the libraries (in this case dbusand dbus-glib), or use the pre-compiled ones from your operating system, in order to link them to the executable.
此外,您需要编译库(在本例中为dbus和dbus-glib),或使用操作系统中预编译的库,以便将它们链接到可执行文件。
You will also need the header files provided with the source, or the "development" packages from your operating system.
您还需要与源代码一起提供的头文件,或操作系统中的“开发”包。
Per example, on my Ubuntu workstation, I can install both the source and the header files like so:
例如,在我的 Ubuntu 工作站上,我可以像这样安装源文件和头文件:
sudo apt-get -y install dbus libdbus-1-dev libdbus-glib-1-2 libdbus-glib-1-dev
Once they are compiled (or properly installed), you proceed to compile the program. You will need to specify the proper include paths and libraries to link to the compiler/linker. Per example, with GCC and my current setup it would be:
一旦它们被编译(或正确安装),您就可以继续编译程序。您需要指定正确的包含路径和库以链接到编译器/链接器。例如,使用 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
This should create an executable a.outin the current directory.
这应该a.out在当前目录中创建一个可执行文件。
Granted, I have a few years of experience with C and Linux so I get figure out all that stuff easily. If you're looking to start with C, you probably should start with something easier though.
当然,我在 C 和 Linux 方面有几年的经验,所以我很容易弄清楚所有这些东西。如果你想从 C 开始,你可能应该从更简单的东西开始。
回答by enthusiasticgeek
Based on 'netcoder's'answer is the program that worked for me.
基于“netcoder”的答案是对我有用的程序。
#include <stdlib.h> // for exit()
#include <dbus/dbus.h> // for dbus_*
#include <dbus/dbus-glib.h> // 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;
}
and 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
Note:This webpage has a good D-bus example https://developer.gnome.org/gio//2.36/GDBusProxy.html
注意:此网页有一个很好的 D-bus 示例https://developer.gnome.org/gio//2.36/GDBusProxy.html
回答by Philip Withnall
Note that libdbus-glib is deprecated, unmaintained and should not be used for accessing D-Bus from C: use GDBusinstead. libdbus-1 is not recommended either: it is maintained, but is a much lower-level API for using D-Bus, and does not have all the convenience features of GDBus.
请注意,libdbus-glib 已弃用、未维护,不应用于从 C 访问 D-Bus:请改用GDBus。也不推荐 libdbus-1:它被维护,但它是一个用于使用 D-Bus 的低级 API,并且不具备 GDBus 的所有便利功能。
As enthusiasticgeek says, there's good GDBus documentation available.
正如热情的人所说,有很好的GDBus 文档可用。
(libdbus-glib and libdbus-1 deliberately not linked to avoid giving them google juice.)
(libdbus-glib 和 libdbus-1 故意不链接以避免给他们谷歌果汁。)
回答by Anton Kovalenko
Looks like you have to include <dbus/dbus-glib.h>separately, as it is not automatically included by <dbus.h>
看起来您必须<dbus/dbus-glib.h>单独包含,因为它不会自动包含在<dbus.h>
回答by MOHAMED
Based on the error returned by your gcc command. The gcc is able to see the <dbus/dbus.h>file (otherwise it will display error message indicating that he is not able to see the header file) but is not able to see some variables that should be exist in this file (‘DBusGConnection'and ‘GError') . May be you are not using the adequate version of dbus
基于您的 gcc 命令返回的错误。gcc 能够看到该<dbus/dbus.h>文件(否则它会显示错误消息,表明他无法看到头文件),但无法看到该文件中应该存在的一些变量(‘DBusGConnection'和‘GError')。可能是您没有使用足够版本的 dbus
and try to use make file instead of one command
并尝试使用 make file 而不是一个命令
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

