當前位置: 華文星空 > 知識

ROS怎麽參照動態庫?

2021-11-18知識

在ROS開發中經常要定義動態庫供其他包呼叫。分享一下,在ROS中建立和使用動態庫的經驗。

一、建立動態庫

1.建立名為lib_test的ROS包,依賴roscpp

catkin_create_pkg lib_test roscpp

2.添加動態庫的表頭檔libheader.h,其他包使用該庫時需要參照

#ifndef LIBHEADER_H #define LIBHEADER_H int LibFunc ( int a , int b ); #endif // LIBHEADER_H

3.添加動態庫的實作檔libmain.cpp

#include "lib_test/libheader.h" #include <ros/ros.h> int LibFunc ( int a , int b ){ ROS_INFO ( "LibFunc is called %d" , a + b ); return a + b ; }

4.修改CMakelist.txt檔幾處關鍵配置,將lib_test包編譯為一個動態庫

#添加要編譯的檔 file ( GLOB_RECURSE SRCS *.cpp ) set ( SOURCE_FILES ${ SRCS } ) ################################### ## catkin specific configuration ## ################################### ## The catkin_package macro generates cmake config files for your package ## Declare things to be passed to dependent projects ## INCLUDE_DIRS: uncomment this if your package contains header files ## LIBRARIES: libraries you create in this project that dependent projects also need ## CATKIN_DEPENDS: catkin_packages dependent projects also need ## DEPENDS: system dependencies of this project that dependent projects also need catkin_package ( INCLUDE_DIRS include #對外釋出動態庫的表頭檔(這裏配錯其他包將無法參照) LIBRARIES lib_test #對外釋出動態庫庫名(這裏配錯其他包將無法參照) # CATKIN_DEPENDS roscpp # DEPENDS system_lib ) #自身編譯需要的表頭檔目錄 include_directories ( # include ${ catkin_INCLUDE_DIRS } ${ CMAKE_CURRENT_SOURCE_DIR } /include ) #將本包編譯成動態庫 add_library ( ${ PROJECT_NAME } ${ SOURCE_FILES } ) target_link_libraries ( ${ PROJECT_NAME } ${ catkin_LIBRARIES } )

5.目錄結構如下

lib_test/ ├── CMakeLists.txt ├── include │ └── lib_test │ └── libheader.h ├── package.xml └── src └── libmain.cpp

二、使用動態庫

1.建立名為uselib的ROS包,依賴roscpp和上面建立的libtest包

catkin_create_pkg use_lib roscpp lib_test

2.修改CMakelist.txt檔幾處關鍵配置,添加對libtest動態庫的依賴

#添加要編譯的檔 file ( GLOB_RECURSE SRCS *.cpp ) set ( SOURCE_FILES ${ SRCS } ) ################################### ## catkin specific configuration ## ################################### ## The catkin_package macro generates cmake config files for your package ## Declare things to be passed to dependent projects ## INCLUDE_DIRS: uncomment this if your package contains header files ## LIBRARIES: libraries you create in this project that dependent projects also need ## CATKIN_DEPENDS: catkin_packages dependent projects also need ## DEPENDS: system dependencies of this project that dependent projects also need catkin_package ( # INCLUDE_DIRS include # LIBRARIES use_lib CATKIN_DEPENDS lib_test roscpp #添加對lib_test動態庫的依賴 # DEPENDS system_lib ) #自身編譯需要的表頭檔目錄 include_directories ( # include ${ catkin_INCLUDE_DIRS } ${ CMAKE_CURRENT_SOURCE_DIR } /include ) #將本包編譯成可執行檔 add_executable ( ${ PROJECT_NAME } ${ SOURCE_FILES } ) target_link_libraries ( ${ PROJECT_NAME } ${ catkin_LIBRARIES } )

3.添加使用動態庫的實作檔main.cpp

#include "lib_test/libheader.h" #include <ros/ros.h> int main ( int argc , char ** argv ){ int result = LibFunc ( 1 , 2 ); ROS_INFO ( "result = %d" , result ); }

4.目錄結構如下

use_lib/ ├── CMakeLists.txt ├── include │ └── use_lib ├── package.xml └── src └── main.cpp

三、編譯執行結果

catkin build lib_test catkin build use_lib source devel/setup.bash rosrun use_lib use_lib [ INFO] [ 1636106179.816962716] : LibFunc is called 3 [ INFO] [ 1636106179.816992236] : result = 3