ROS笔记:引用同一工作空间下其他包中的 .h 文件

简略地记录一下自己解决这个小问题的过程,折腾了好几天。

引用同一工作空间下其他package的‘.h’文件,一种方法是创建library生成动态链接库‘.so’:
[1]https://answers.ros.org/question/33779/how-do-i-link-an-executable-with-library-in-other-package/?answer=33781#post-id-33781.
[2]https://blog.csdn.net/wawayu_0/article/details/79410237?utm_source=blogxgwz2.
[3]https://blog.csdn.net/Will_Ye/article/details/79587075.
另一种方法是直接引用‘.h’文件:
[1]https://blog.csdn.net/zhixiongning/article/details/81910857.
[2]http://question2464.rssing.com/browser.php?indx=39696734&item=61.

自己再整理一下步骤

B_package调用A_package中的‘.h’文件
A_package:
1、修改CMakeList.txt文件:

1)catkin_package(
   INCLUDE_DIRS include
   #LIBRARIES motordriver
   #CATKIN_DEPENDS roscpp rospy std_msgs
   #DEPENDS system_lib )

2)include_directories( include ${catkin_INCLUDE_DIRS} )

B_package:
1、修改package.xml文件(这项好像不修改也能编译成功):
  1. A_package

  2. A_package

2、修改CMakeList.txt文件:
  1. find_package( catkin REQUIRED COMPONENTS
     roscpp
     rospy
     std_msgs
     A_package ) (这一步很重要!!!)

  2. catkin_package( INCLUDE_DIRS include
      #LIBRARIES tracking
      #CATKIN_DEPENDS roscpp rospy std_msgs
      #DEPENDS system_lib )   (这一步好像也是不修改也OK)

  3. include_directories(include ${catkin_INCLUDE_DIRS})

3、然后B_package中的‘.cpp’文件引用的时候需要指出‘.h’文件的所属功能包:

#include

最后贴上自己当时遇到错误的提示与关键的修改:
错误1:Project ‘xxx’ tried to find library ‘A_package’. The library is neither a target nor built/installed properly. Did you compile project ‘A_package’? Did you find_package() it before the subdirectory containing its code is included?

ROS笔记:引用同一工作空间下其他包中的 .h 文件_第1张图片
修改:A_package的CMakeList.txt文件:
 catkin_package(
  INCLUDE_DIRS include
  #LIBRARIES A_package (这里要注释掉)
  CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
  DEPENDS system_lib )

错误2:fatal error: A_package/xx.h: 没有那个文件或目录

ROS笔记:引用同一工作空间下其他包中的 .h 文件_第2张图片
修改:需要被引用的‘.h’文件要放在A_package/include/A_package中。之前‘.h’放在A_package/include里导致一直报错说找不到。

你可能感兴趣的:(ROS)