0

I'm creating a project with cmake and c where I need to include headers from different folders,

#include <dbus/dbus.h>
#include <audacious/audctrl.h>
#include <audacious/dbus.h>
#include <pidgin/pidgin.h>

And I added these lines to the cmake cofig file:

target_include_directories(untitled PRIVATE
    /usr/include/dbus-1.0/;/usr/lib/x86_64-linux-gnu/dbus-1.0/include/;/usr/include/glib-2.0/;
    /usr/lib/x86_64-linux-gnu/glib-2.0/include/;/usr/include/gtk-3.0/;/usr/include/pango-1.0/;/usr/include/cairo/;
    /usr/include/gdk-pixbuf-2.0/;/usr/include/atk-1.0/
)

Here is some part of the code:

GError *gError = NULL;
DBusGConnection *dBusGConnection = dbus_g_bus_get(DBUS_BUS_SESSION,gError);
DBusGProxy *dBusGProxy = dbus_g_proxy_new_for_name(dBusGConnection,AUDACIOUS_DBUS_SERVICE,AUDACIOUS_DBUS_PATH,AUDACIOUS_DBUS_INTERFACE);
**audacious_remote_get_main_volume(dBusGProxy);**

but when i try to build the project it returns this error:

 /usr/bin/ld: CMakeFiles/untitled.dir/main.c.o: en la función `main':
`main.c:(.text.startup+0x9): referencia a `dbus_g_bus_get' sin definir
/usr/bin/ld: main.c:(.text.startup+0x22): referencia a `dbus_g_proxy_new_for_name' sin definir
/usr/bin/ld: main.c:(.text.startup+0x2a): referencia a `audacious_remote_get_main_volume' sin definir
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/untitled.dir/build.make:84: untitled] Error 1
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/untitled.dir/rule] Error 2
make: *** [Makefile:118: untitled] Error 2

I've tried all examples on the internet to try to make it work and nothing. I'm totally new in this world of C/C++ I come from Java and there is no need of doing these sort of things.

Please any help would be appreciated.

PedroVLP
  • 15
  • 5
  • The "undefined reference" has nothing common with the *include directories*. You need to link with appropriate **libraries** for overcome this error. In CMake linking is performed with command [target_link_libraries](https://cmake.org/cmake/help/latest/command/target_link_libraries.html). – Tsyvarev Nov 23 '20 at 07:47

1 Answers1

0

When you say target_include_directories it will find the corresponding headers for you. Hence, when you compiled and it didn't complain about your headers it's because you included their paths to be found during compilation.

My hunch is you're not specifying the library files during static linking. When you don't have a definition for things like dbus_g_bus_get(), it's because the actual definition for those files live in libraries. Libraries can be statically linked or dynamically linked, and have prefixes like .a and .so. You can find these files in your /usr/lib, /lib or /usr/local if you've installed them through your distro's package manager like apt-get or yum or you built them from source.

To fix your problem, you probably need to add the following to your cmake file:

target_link_libraries(untitled <A> <B> ...)

where <A> could be a path prefixed with -L or could be the actual absolute path to a library file.

OneRaynyDay
  • 3,658
  • 2
  • 23
  • 56
  • And is there some way of adding directories of libraries instead file by file? I mean if I have in linux all my .so libraries in the directory /usr/lib/x86_64-linux-gnu/ and I want to import them all is there a way I can do that? And what if it is not only one directory if I have those libraries in different directories like for example /usr/lib/x86_64-linux-gnu/ and /usr/lib/i386-linux-gnu how can I do that? And thanks for the reply. – PedroVLP Nov 23 '20 at 19:13
  • Hey, as I said you can use a path prefixed with `-L`. for example, `-L/here/is/some/lib` where the library is actually at `/here/is/some/lib/lib.a` – OneRaynyDay Nov 24 '20 at 02:02
  • Oh I'm sorry missed that. Thanks a lot for the answer. – PedroVLP Nov 24 '20 at 02:41
  • No worries :) glad to help – OneRaynyDay Nov 24 '20 at 02:56