Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure DBus dependencies with CMake

Tags:

c++

cmake

dbus

I am new to CMake and DBus. I am following along the guide here and make a basic program compile and execute.

The first problem that I ran into was my program will not find

<dbus/dbus.h>

I got around that issue by adding some include directories to my CMakeList.txt. Currently, my CMakeLists.txt looks like this:

...

include_directories(/usr/lib/)
include_directories(/usr/include/dbus-1.0/)
include_directories(/usr/lib/x86_64-linux-gnu/dbus-1.0/include)
include_directories(/usr/include/glib-2.0)
include_directories(/usr/lib/x86_64-linux-gnu/glib-2.0/include/)

set (LIBS
  dbus-1
  dbus-glib-1
)

add_executable(mydbus mydbus.cpp)

target_link_libraries(mydbus ${LIBS} )

Now, my program is complaining about not being able to find dbus-arch-deps.h

/usr/include/dbus-1.0/dbus/dbus.h:29:33: fatal error: dbus/dbus-arch-deps.h: No such file or directory
 #include <dbus/dbus-arch-deps.h>

I know that the solution for this is to use proper command line flags or pkg-config. As discussed here and numerous other posts.

However, I do not know how to configure CMakeLists.txt to have similar effect.

My guess would be to add something like find_package(dbus-1) to CMakeLists.txt. And if that is correct, I am going to have to write my own Finddbus-1.cmake. Does this sound correct? Or is there an easier way?

I will appreciate any pointers.

like image 903
sdevikar Avatar asked Dec 15 '22 04:12

sdevikar


1 Answers

You may get an existing FindDBus.cmake script (e.g., this one), copy it into your project, and use as

find_package(DBus REQUIRED)
# Use results of find_package() call.
include_directories(${DBUS_INCLUDE_DIRS})

add_executable(mydbus mydbus.cpp)

target_link_libraries(mydbus ${DBUS_LIBRARIES})

Alternatively, as you know pkgconfig can find DBus, you may use CMake module PkgConfig. Actually, FindDBus.cmake script, referenced above, uses PkgConfig module in its implementation. Possible usage could be:

find_package(PkgConfig REQUIRED) # Include functions provided by PkgConfig module.

pkg_check_modules(DBUS REQUIRED dbus-1) # This calls pkgconfig with appropriate arguments
# Use results of pkg_check_modules() call.
include_directories(${DBUS_INCLUDE_DIRS})
link_directories(${DBUS_LIBRARY_DIRS})

add_executable(mydbus mydbus.cpp)

target_link_libraries(mydbus ${DBUS_LIBRARIES})

However, using link_directories is not recommended, it is better to use absolute paths to libraries in target_link_libraries() call. That is why it is better to combine pkg_check_modules with find_library, as it is done in the referenced Find script. That answer describes generic way for use result of pkgconfig in CMake.

like image 187
Tsyvarev Avatar answered Jan 04 '23 07:01

Tsyvarev