1. 创建功能包,例如功能包的名字为 kalman_filter
catkin_create_pkg kalman_filter roscpp rospy std_msgs
2. 在创建好的功能包中写好.cpp文件和.h文件
3. 修改kalman_filter功能包的CMakeLists.txt文件
catkin_package(
INCLUDE_DIRS include
#LIBRARIES kalman_filter
#CATKIN_DEPENDS roscpp rospy std_msgs
#DEPENDS system_lib
)
include_directories(
include
${catkin_INCLUDE_DIRS}
#${EIGEN3_INCLUDE_DIRS}
)
add_library(kalman_filter_lib src/kalman_filter.cpp) # kalman_filter_lib为要生成的动态库的名称
add_dependencies(kalman_filter_lib ${catkin_EXPORTED_TARGETS})
target_link_libraries(kalman_filter_lib
${catkin_LIBRARIES}
)
4. 在其他功能包中调用,修改要调用动态库的CMakeLists.txt和package.xml
CMakeLists.txt中修改如下
find_package(catkin REQUIRED COMPONENTS
kalman_filter
# 其他依赖...
)
target_link_libraries(your_node
${catkin_LIBRARIES}
kalman_filter_lib # 这个名字要和前面编译生成的动态库的名称一致
# 其他库...
)
include_directories(
include
${catkin_INCLUDE_DIRS}
#${EIGEN3_INCLUDE_DIRS}
kalman_filter_lib
)
package.xml中修改
<build_depend>kalman_filter</build_depend>
<exec_depend>kalman_filter</exec_depend>