dexterous
dexterous

Reputation: 6526

Dbus-API - acquire service linking error in c /Linux?

I am trying to compile and run an example for C-DBUS. Here is my DBUS-Server.

#include <dbus/dbus.h>

/** How to compile dbus- gcc `pkg-config --cflags --libs dbus-1` dbus_server.c -o dbus_server.out **/
int main()
{

  DBusError error;
  DBusConnection *conn;

  /** Initialize the bus.. **/
  dbus_error_init(&error);

  /** Get the system bus...........**/
  conn = dbus_bus_get(DBUS_BUS_SYSTEM,&error);

  if( !conn)
   {
      printf("ERROR................................in getting bus...\r\n");

      return -1;  


    }


  /** acquire service **/


  dbus_bus_acquire_service(conn, "org.shreyas.tune",0,&error);


  if( dbus_error_is_set(&error))
  {
     dbus_connection_disconnect(conn);  
     printf("Disconnect bus... some problem in acquiring service..\r\n");
     return -1;  
  }

  /** Let's send a signal **/

  DBusMessage *msg;
  DBusMessageIter iter;

  /** My signal name is SJ_FIRST_EXAMPLE **/
  msg = dbus_message_new_signal("org/shreyas/tune/attr","org.shreyas.tune.attr","SJ_FIRST_EXAMPLE");

  /** Let fill the payload now **/

  dbus_message_iter_init(msg,&iter);
  dbus_message_iter_append_string(&iter,"This is my first example");

  /** send the message **/

  if(!dbus_connection_send( conn, msg,NULL) )
  {
     printf("Error in sending the message\r\n");
     return -1;  
  }

  dbus_message_unref(msg);

  dbus_connection_flush(conn);




  return 0; 




}
$gcc `pkg-config --cflags --libs dbus-1` dbus_server.c -o dbus_server.out
dbus_server.c: In function ‘main’:
dbus_server.c:19:4: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
dbus_server.c:35:3: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
dbus_server.c:56:3: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
/tmp/ccGFwJyC.o: In function `main':
dbus_server.c:(.text+0x6a): undefined reference to `dbus_bus_acquire_service'
dbus_server.c:(.text+0x86): undefined reference to `dbus_connection_disconnect'
dbus_server.c:(.text+0xe4): undefined reference to `dbus_message_iter_append_string'
collect2: ld returned 1 exit status

How to resolve the linking error here? Please help.

Upvotes: 0

Views: 1423

Answers (2)

Philip Withnall
Philip Withnall

Reputation: 5713

JB0x2D1’s answer is correct for solving your linking problem.

It is highly recommended that you use a D-Bus library other than libdbus, however, as libdbus is fiddly to use correctly. If possible, use GDBus or QtDBus instead, as they are much higher-level bindings which are easier to use. If you need a lower-level binding, sd-bus is more modern than libdbus.

Upvotes: 0

JB0x2D1
JB0x2D1

Reputation: 930

Try this

gcc `pkg-config --cflags dbus-1` dbus-server.c `pkg-config --libs dbus-1` -o dbus-server.out

The cflags and libs go to the compiler and linker respectively. For some reason gcc needs the cflags before the .c file and the libs after it in two separate pkg-config calls.

Also try using a makefile or a script so you don't have to type that whole mess every time you recompile.

Upvotes: 3

Related Questions