cmake 如何添加依赖库

1 说明

cmake 经常需要添加依赖库,但是不知如何确定依赖的库名称,如何编写

2 环境

ubuntu 机器

3 依赖方法

以 dbus 为例

3.1、确定库的名字

通过dpkg 命令搜索库名,如下所示,确定libdbus-1-dev 为自己需要的版本

user@ebaaa3966495:/home/sky/develop/robot$ dpkg -l |grep dbus
ii  at-spi2-core                                    2.36.0-2                             amd64        Assistive Technology Service Provider Interface (dbus core)
ii  dbus                                            1.12.16-2ubuntu2.3                   amd64        simple interprocess messaging system (daemon and utilities)
ii  dbus-user-session                               1.12.16-2ubuntu2.2                   amd64        simple interprocess messaging system (systemd --user integration)
ii  libdbus-1-3:amd64                               1.12.16-2ubuntu2.3                   amd64        simple interprocess messaging system (library)
ii  libdbus-1-dev:amd64                             1.12.16-2ubuntu2.3                   amd64        simple interprocess messaging system (development headers)
ii  libqt5dbus5:amd64                               5.12.8+dfsg-0ubuntu2.1               amd64        Qt 5 D-Bus module
ii  python3-dbus                                    1.2.16-1build1                       amd64        simple interprocess messaging system (Python 3 interface)

3.2 查看库支持cmake 的添加方式

一般camke 支持 find_package、pkg_check_modules 两种方式

user@ebaaa3966495:/home/sky/develop/robot$ dpkg -L libdbus-1-dev
/.
/usr
/usr/include
/usr/include/dbus-1.0
/usr/include/dbus-1.0/dbus
/usr/include/dbus-1.0/dbus/dbus-address.h
/usr/lib/x86_64-linux-gnu/cmake/DBus1/DBus1Config.cmake
/usr/lib/x86_64-linux-gnu/cmake/DBus1/DBus1ConfigVersion.cmake
/usr/lib/x86_64-linux-gnu/dbus-1.0
/usr/lib/x86_64-linux-gnu/dbus-1.0/include/dbus/dbus-arch-deps.h
/usr/lib/x86_64-linux-gnu/libdbus-1.a
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/dbus-1.pc

其中,关键词 .cmake 代表支持 find_package 方式
pkgconfig 代表支持 pkg_check_modules 方式

/usr/lib/x86_64-linux-gnu/cmake/DBus1/DBus1Config.cmake
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/dbus-1.pc

所以,dbus-1 支持2种方式

3.3 编写对应的cmake 库依赖

find_package(DBus1 REQUIRED)    # 自动查找依赖库

# 最终的执行文件添加依赖
ament_target_dependencies(
  DBus1
)
ament_package()

3.4 延伸

同理查看glib2.0 的依赖方式

user@ebaaa3966495:/home/sky/develop/robot$ dpkg -l |grep glib
ii  gir1.2-glib-2.0:amd64                           1.64.1-1~ubuntu20.04.1               
ii  libavahi-glib1:amd64                            0.7-4ubuntu7.1                       
ii  libglib2.0-0:amd64                              2.64.6-1~ubuntu20.04.4               amd64        GLib library of C routines
ii  libglib2.0-bin                                  2.64.6-1~ubuntu20.04.4               amd64        Programs for the GLib library
ii  libglib2.0-data                                 2.64.6-1~ubuntu20.04.4               all          Common files for GLib library
ii  libglib2.0-dev:amd64                            2.64.6-1~ubuntu20.04.4               amd64        Development files for the GLib library
ii  libglib2.0-dev-bin                              2.64.6-1~ubuntu20.04.4                              amd64        Support library to use the Qualcomm MSM Interface (QMI) protocol

user@ebaaa3966495:/home/sky/develop/robot$ dpkg -L  libglib2.0-dev
/.
/usr
/usr/include
/usr/include/gio-unix-2.0
/usr/include/gio-unix-2.0/gio
...
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/gio-2.0.pc 

glib2.0 只有 pkgconfig支持,所以依赖的方式

# 其中 glib-2.0 为具体的库名
pkg_check_modules(glib REQUIRED glib-2.0)

ament_target_dependencies(
  DBus1
  glib
)
ament_package()

你可能感兴趣的:(cmake,cmake)