嗨,我正在尝试让 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/)
我做错了什么??
在这种情况下,要了解 CMake 的第一件事是不要使用
include_directories
来包含任何具有硬编码路径的系统目录(这就是您现在正在做的事情)。您应该做的是使用 CMake FindPkgConfig
模块,它将调用 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_DIRS} )