1

I am new to using DBUS and would like to compile a program that uses glib-2.0.

However I get an error. Here is my output from calling make:

g++ -I. -Wall -std=c++11 main.c `pkg-config --cflags --libs glib-2.0 dbus-1 
dbus-glib-1` -o main.o
main.c: In function ‘int main(int, char**)’:
main.c:12:15: warning: variable ‘client’ set but not used [-Wunused-but-set-variable]
GDBusClient *client;
           ^
/tmp/ccNkl0Zc.o: In function `main':
main.c:(.text+0x20): undefined reference to `g_dbus_setup_bus'
main.c:(.text+0x44): undefined reference to `g_dbus_client_new'
collect2: error: ld returned 1 exit status
makefile:9: recipe for target 'main.o' failed

make: *** [main.o] Error 1

I tried following the instructions found on gnome.org https://developer.gnome.org/glib/stable/glib-compiling.html

This is my make file:

CC=g++

CFLAGS=-I. -Wall -std=c++11 \
       -I/usr/include/dbus-1.0 \
       -I/usr/lib/arm-linux-gnueabihf/dbus-1.0/include/ \
       -I/usr/lib/arm-linux-gnueabihf/glib-2.0/include \
       -I/usr/include/glib-2.0


 OBJ = main.o

 default: program

 main.o: main.c hello.h
   $(CC) $(CFLAGS) main.c `pkg-config --cflags --libs glib-2.0` -o main.o

program: $(OBJ)
   $(CC) $(OBJ) $(CFLAGS) -o program 


clean:
  -rm -f main.o
  -rm -f program

This is my main:

#include <cstddef>
#include <cstdio>
#include <iostream>
#include <glib.h>
#include <dbus/dbus.h>
#include <gdbus/gdbus.h>

static DBusConnection *dbus_conn;

int main(int argc, char * argv[]){
     GDBusClient *client;

     dbus_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);

     client = g_dbus_client_new(dbus_conn, "org.bluez", "/org/bluez");
     return 0;
}

Why am I getting undefined reference to these calls?

Kyle
  • 43
  • 1
  • 6
  • Why are you hardcoding the `-I` in `CLFAGS` when you use `pkg-config` anyway? – Pablo Mar 16 '18 at 02:06
  • I was originally not using the `pkg-config` so I never changed my `CFLAGS` I don't believe that is what is causing the issue. – Kyle Mar 16 '18 at 02:23
  • Perhaps not, but it's not that correct either, if the paths that `pkg-config` return are not the same. What is the error message? Is it complaining about the undefined references or is it also complaining that the `.so` file is not found? – Pablo Mar 16 '18 at 02:26
  • It doesn't seem to be complaining about .so file. I added a image of the terminal output. I also changed the` CFLAGS` to: `CFLAGS=I. -Wall -std=c+11`. Still got the same error. – Kyle Mar 16 '18 at 02:33
  • Please don't post pictures of the output, make a copy and paste in your question. I cannot do text-select from a picture. Form what I've seen doing a quick google search is that people are also using `dbus-1 dbus-glib-1` in the `pkg-config` line. Try adding those two. – Pablo Mar 16 '18 at 02:45
  • Still no luck when adding those two lines. – Kyle Mar 16 '18 at 02:55
  • I cannot compile this code, my system does not have `gdbus/gdbus.h` and from what I've seen in other SO questions, this file is not present in any library as it is part of bluez. See https://stackoverflow.com/q/37495335/1480131 – Pablo Mar 16 '18 at 02:57

1 Answers1

1

Had to simplify it a bit for my system, but this works for me:

main.o: main.c
        $(CC) $(CFLAGS) main.c `pkg-config --cflags glib-2.0` -c -o main.o

program: $(OBJ)
        $(CC) $(OBJ) $(CFLAGS) `pkg-config --libs glib-2.0` -o program

The first change is to your generation of main.o. You didn't have the -c flag, so main.o is a full program.

The second change is to only use the cflags when compiling, and use the ldflags when linking. I have the glib library when I run ldd:

ldd program 
    linux-vdso.so.1 (0x00007ffd489ae000)
    libglib-2.0.so.0 => /usr/lib64/libglib-2.0.so.0 (0x00007fc29cf4a000)
    libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/7.3. /libstdc++.so.6 (0x00007fc29cb45000)
    libm.so.6 => /lib64/libm.so.6 (0x00007fc29c83d000)
    libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libgcc_s.so.1 (0x00007fc29c626000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fc29c277000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fc29c005000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc29bde5000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fc29d25d000) [/tmp/glib] pkg-config --libs glib-2.0
Stephen Newell
  • 7,330
  • 1
  • 24
  • 28
  • Unrelated to your question, but it's odd to have a C++ file with the extension '.c'. I'd suggest renaming it to a more common extension used by C++ files (e.g., cpp or cxx). – Stephen Newell Mar 16 '18 at 03:13
  • Hi Stephen, thanks for taken a stab at the problem. I did what you recommended. However I still get the same error. I did however find a program under 'bluez' called 'bluetooth-player' that is already compiled which uses the glib, and dbus, and when using the command `ldd bluetooth-play' I get the a similar output that you did. – Kyle Mar 16 '18 at 04:03
  • I may have simplified too much then :) (I don't have `gdbus.h`, and since your error is a missing library I assumed it was in the linker step). You'll probably have to do some research to figure out what the proper library is that provides the missing symbols (maybe it came with whatever provided the header). Once you have that, add it to the link line with something like `-lgdbus` (guessing at the library name). – Stephen Newell Mar 16 '18 at 04:10
  • So I'm an idiot, but just realized you can probably use `ldd` to narrow down the library. Check the libraries that `bluetooth-player` is linked against, since once of those has the symbols you're missing. – Stephen Newell Mar 16 '18 at 04:24
  • Thanks Stephen I will take a look. I do have the gdbus.h file, and the directory with associated files that i got from bluez. I went through the Makefile that bleuz used to build bluetooth-player, among other programs. I found this line: `tools_bluetooth_player_LDADD = gdbus/libgdbus-internal.la \ -lglib-2.0 -ldbus-1 -lreadline`. Does this mean anything? – Kyle Mar 16 '18 at 04:28
  • Yup. You probably need to add `-lgdbbus-internal` and `-ldbus-1`. It looks like `libgdbus-internal` is packaged with `bleuz`, so I'm not sure how you'll get that in your project if you don't already have it. It may have come with the headers though (they usually do), in which case you should be fine. – Stephen Newell Mar 16 '18 at 04:35