【问题标题】:How to configure DBus dependencies with CMake如何使用 CMake 配置 DBus 依赖项
【发布时间】:2016-11-25 16:48:39
【问题描述】:

我是 CMake 和 DBus 的新手。我按照指南here 编译和执行一个基本程序。

我遇到的第一个问题是我的程序找不到

<dbus/dbus.h>

我通过在我的 CMakeList.txt 中添加一些包含目录来解决这个问题。 目前,我的 CMakeLists.txt 如下所示:

...

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} )

现在,我的程序抱怨找不到 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>

我知道解决方案是使用正确的命令行标志或 pkg-config。正如here 和许多其他帖子所讨论的那样。

但是,我不知道如何配置 CMakeLists.txt 以获得类似的效果。

我的猜测是将 find_package(dbus-1) 之类的内容添加到 CMakeLists.txt 中。如果这是正确的,我将不得不编写自己的 Finddbus-1.cmake。这听起来正确吗?还是有更简单的方法?

我将不胜感激。

【问题讨论】:

    标签: c++ cmake dbus


    【解决方案1】:

    您可能会得到一个现有的FindDBus.cmake 脚本(例如,this one),将其复制到您的项目中,然后用作

    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})
    

    另外,如您所知 pkgconfig 可以找到 DBus,您可以使用 CMake 模块 PkgConfig。实际上,上面引用的FindDBus.cmake 脚​​本在其实现中使用了 PkgConfig 模块。可能的用法是:

    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})
    

    不过,不推荐使用link_directories,在target_link_libraries()调用中最好使用库的绝对路径。这就是为什么最好将pkg_check_modulesfind_library 结合起来,因为它是在引用的Find 脚本中完成的。 That answer 描述了在 CMake 中使用 pkgconfig 结果的通用方式。

    【讨论】:

    • 感谢您花时间回答。不幸的是,该解决方案对我不起作用。 CMake 阶段进展顺利,它会生成一个 Makefile。但是,当我做时,那是我得到那个错误的时候。我尝试包含/usr/lib/i386-linux-gnu/dbus-1.0/include/dbus,因为这是该文件所在的位置。但我仍然得到错误。
    • 文件包含为&lt;dbus/dbus-arch-deps.h&gt;,因此包含路径不应包含尾随dbus 子目录。顺便说一句,您机器上标题 dbus-arch-deps.h 的确切路径是什么?
    • 非常感谢!这个建议似乎奏效了。此外,我还必须包含 glibconfig.h 的目录路径。我只是想知道..我做事的方式正确吗?如,根据程序的复杂性,可能有几十个头文件依赖项。就我而言, find_package(PkgConfig) 没有帮助。那么,如何配置我的 CMake 以实现与在命令行上执行 pkg-config --cflags --libs glib-2.0 之类的操作相同的行为?
    • 我已经更新了我的答案,其中包含有关使用 find_package()pkg_check_modules 搜索 DBus 的更详细示例。 pkg_check_modules 也可以应用于 glib-2.0,这将具有 pkg-config 的效果。
    猜你喜欢
    • 2015-10-26
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 2015-10-09
    • 2017-02-23
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多