首页 > 解决方案 > Cmake 在编译期间拒绝找到 dbus-0

问题描述

嗨,我试图让 cmake 找到 dbus-1

当我尝试编译时,我不断收到此错误

--   Checking for module 'dbus-1'
--   No package 'dbus-1' found

我试过这个命令

pkg-config --cflags dbus-glib-1

我得到输出

-I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include

我编辑了 CMakeLists.txt 并添加了

include_directories(/usr/include/dbus-1.0/)

我究竟做错了什么??

标签: cmakedbus

解决方案


在这种情况下,首先要了解 CMake 是不要使用include_directories包含硬编码路径的任何系统目录(这就是您现在正在做的事情)。您应该做的是使用 CMakeFindPkgConfig模块,该模块将调用 pkg-config 并为您获取这些包含目录。

为此,应该使用以下类似的方法。

include( FindPkgConfig )
pkg_check_modules( dbus REQUIRED dbus-1 )

# Add the include directories to our target executable/shared object.
# In this case, our target is called 'executable' and must have been
# created by a previous call to either 'add_executable' or 'add_library'
target_include_directories( executable PUBLIC ${dbus_INCLUDE_DIRECTORIES} )

推荐阅读