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 paragraph that 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 ?
My OS is Ubuntu 10.04 32bit. I installed the libdbus-glib-1-dev
package. I tried to add #include <dbus/dbus.h>
at the beginning of the source file, and to compile with
$ 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.
Note that libdbus-glib is deprecated, unmaintained and should not be used for accessing D-Bus from C: use GDBus instead. 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.
As enthusiasticgeek says, there’s good GDBus documentation available.
(libdbus-glib and libdbus-1 deliberately not linked to avoid giving them google juice.)
Looks like you have to include
<dbus/dbus-glib.h>
separately, as it is not automatically included by<dbus.h>
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.
Looking at the tutorial, I see that it only contains a
main
function. As such, you will need to add the proper#include
directives in order for this to work:Also, you will need to compile the libraries (in this case
dbus
anddbus-glib
), or use the pre-compiled ones from your operating system, in order to link them to the executable.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:
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:
This should create an execute
a.out
in the current directory.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.
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 dbusand try to use make file instead of one command
Based on 'netcoder's' answer is the program that worked for me.
and Makefile
Note: This webpage has a good D-bus example https://developer.gnome.org/gio//2.36/GDBusProxy.html