ROS::同一个WorkSpace的Package相互调用

同一个WorkSpace的Package相互调用


假设我有

  • A_package : 这是一个自己写的库
  • B_package : 这是另外的可执行package

目标

在B_package中调用A_package中所实现的库


 A_package

目录结构

plus.h

// plus.h
#ifndef PLUS_H
#define PLUS_H

#include <iostream>
class plus
{
public:
    plus();
};

#endif // PLUS_H

plus.cpp

// plus.cpp
#include "aaa/plus.h"

plus::plus()
{
    std::cout<<"plus"<<std::endl;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(aaa)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
    roscpp
    )

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)

catkin_package(
    # 这三个必须要有
    INCLUDE_DIRS include
    LIBRARIES aaa
    CATKIN_DEPENDS roscpp
    #  DEPENDS system_lib
    )

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
    include
    ${catkin_INCLUDE_DIRS}
    )

add_library(aaa src/plus.cpp)

install(
    TARGETS ${PROJECT_NAME}
    ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
    RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
    )
install(
    DIRECTORY include/${PROJECT_NAME}/
    DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
    )

package.xml

<?xml version="1.0"?>
<package format="2">
  <name>aaa</name>
  <version>0.1.0</version>
  <description>The aaa package</description>

  <maintainer email="autoware@todo.todo">autoware</maintainer>

  <license>Apache 2.0</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <exec_depend>roscpp</exec_depend>

  <!-- The export tag contains other, unspecified, tags -->
  <!--  这里必须要有-->
  <export>
    <cpp cflags="-I${prefix}/include" lflags="-L${prefix}/lib -laaa"/>
  </export>

</package>

 B_package

目录结构

aaa_test.cpp

#include <ros/ros.h>
#include "aaa/plus.h"

int main(int argc, char **argv)
{
    ros::init(argc, argv, "aaa_test");
    ros::NodeHandle nh;

    plus t;

    ROS_INFO("Hello world!");
}

CMakeList.txt

cmake_minimum_required(VERSION 2.8.3)
project(aaa_test)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

find_package(catkin REQUIRED COMPONENTS
  #aaa 必须要有
  aaa
  roscpp
)

catkin_package(
#  CATKIN_DEPENDS aaa roscpp
#  DEPENDS system_lib
)

###########
## Build ##
###########

include_directories(
#  include
  ${catkin_INCLUDE_DIRS}
)

add_executable(aaa_test aaa_test.cpp)
target_link_libraries(${PROJECT_NAME}
    ${catkin_LIBRARIES}
    )

package.xml

<?xml version="1.0"?>
<package format="2">
  <name>aaa_test</name>
  <version>0.0.0</version>
  <description>The aaa_test package</description>

  <!-- One maintainer tag required, multiple allowed, one person per tag -->
  <!-- Example:  -->
  <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
  <maintainer email="autoware@todo.todo">autoware</maintainer>


  <!-- One license tag required, multiple allowed, one license per tag -->
  <!-- Commonly used license strings: -->
  <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <exec_depend>roscpp</exec_depend>

  <!--  三个都要 -->
  <build_depend>aaa</build_depend>
  <build_export_depend>aaa</build_export_depend>
  <exec_depend>aaa</exec_depend>

  <export>
    <!-- Other tools can request additional information be placed here -->
  </export>
</package>

参考

https://roboticsbackend.com/ros-include-cpp-header-from-another-package/

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值