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="[email protected]">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="[email protected]">Jane Doe</maintainer> -->
  <maintainer email="[email protected]">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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章