Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a basic D-Bus/glib example?

Tags:

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.

like image 405
tvuillemin Avatar asked Jan 10 '13 17:01

tvuillemin


People also ask

How do I create a dbus service?

All clients and services exchange messages via bus, and bus is the only "server" - clients and services are "clients" here. Bus assigns name to a client automatically (it's usually ":some_number"). You can ask bus to assign your name with RequestName message of org. freedesktop.

How is Dbus implemented?

A signal in DBus happens as follows: A signal message is created and sent to the bus daemon. When using the low-level API this may be done manually, with certain bindings it may be done for you by the binding when a native object emits a native signal or event.

What is a D-bus interface?

In computing, D-Bus (short for "Desktop Bus") is a message-oriented middleware mechanism that allows communication between multiple processes running concurrently on the same machine.

What is a Dbus command?

The dbus-send command is used to send a message to a D-Bus message bus. See http://www.freedesktop.org/software/dbus/ for more information about the big picture.


1 Answers

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:

#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 dbus and dbus-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:

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 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.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.

like image 95
netcoder Avatar answered Sep 22 '22 07:09

netcoder