Compiling C program with dbus header files

I am trying to compile a C program with these headers: http://pastebin.com/SppCXb0U , on Ubuntu. At first I was not lucky at all, but after reading pkg-config I created this line:

gcc `pkg-config --cflags --libs dbus-1` `pkg-config --cflags --libs glib-2.0` signals-tutorial.c 

However, it still does not work and gives me this error:

 /tmp/cc3BkbdA.o: In function `filter_example': signals-tutorial.c:(.text+0x1a3): undefined reference to `dbus_connection_setup_with_g_main' /tmp/cc3BkbdA.o: In function `proxy_example': signals-tutorial.c:(.text+0x29a): undefined reference to `g_type_init' signals-tutorial.c:(.text+0x2b3): undefined reference to `dbus_g_bus_get' signals-tutorial.c:(.text+0x323): undefined reference to `dbus_g_proxy_new_for_name' signals-tutorial.c:(.text+0x369): undefined reference to `dbus_g_proxy_add_signal' signals-tutorial.c:(.text+0x38a): undefined reference to `dbus_g_proxy_connect_signal' collect2: ld returned 1 exit status 

I'm not sure what to do next.

====================================

Good explanation - thanks. However, I cannot get it to work. Running your command above (with the addition) gives the following result

 gcc `pkg-config --cflags dbus-1` \ > `pkg-config --cflags glib-2.0` \ > signals-tutorial.c \ > `pkg-config --libs dbus-1` \ > `pkg-config --libs glib-2.0` /tmp/ccjN0QMh.o: In function `filter_example': signals-tutorial.c:(.text+0x1a3): undefined reference to `dbus_connection_setup_with_g_main' /tmp/ccjN0QMh.o: In function `proxy_example': signals-tutorial.c:(.text+0x29a): undefined reference to `g_type_init' signals-tutorial.c:(.text+0x2b3): undefined reference to `dbus_g_bus_get' signals-tutorial.c:(.text+0x323): undefined reference to `dbus_g_proxy_new_for_name' signals-tutorial.c:(.text+0x369): undefined reference to `dbus_g_proxy_add_signal' signals-tutorial.c:(.text+0x38a): undefined reference to `dbus_g_proxy_connect_signal' collect2: ld returned 1 exit status 
+6
source share
1 answer

Your problem is not in the header files; your problem is with the libraries; complaints about "undefined links" usually come from the linker. You need to set the library configuration parameters after the source file:

 gcc `pkg-config --cflags dbus-glib-1` \ `pkg-config --cflags dbus-1` \ `pkg-config --cflags glib-2.0` \ signals-tutorial.c \ `pkg-config --libs dbus-glib-1` \ `pkg-config --libs dbus-1` \ `pkg-config --libs glib-2.0` 

The --libs parameter creates a series of -l flags for the compiler; the compiler passes them to the linker. The linker will resolve characters from left to right, starting from the object file (or, in this case, close to the source file), so all -l libraries should follow your source file.

+12
source

Source: https://habr.com/ru/post/886438/


All Articles