Ask Your Question
0

Inherit cpp class from external file in two packages

asked 2016-05-26 16:09:30 -0500

mc123 gravatar image

updated 2016-06-03 10:34:55 -0500

My file structure is as follows:

catkin_workspace/src/
                     package/
                             subpackage_1/src/subpackage_1.cpp
                             subpackage_2/src/subpackage_2.cpp
                             package (Metapackage)

I define a class in each of these .cpp files, and each one contains an instance of the same base class. Where should I save this base class to conform with the ROS C++ Style Guide? And what would I need to add to each subpackage_#'s CMakeLists.txt? Thanks.

EDIT: It's working. I didn't modify the structure of the packages at all. Here are the contents of catkin_workspace/src/package/subpackage_3/CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.3)
project(subpackage_3)
SET(CMAKE_BUILD_TYPE RELEASE)

find_package(catkin REQUIRED COMPONENTS
  roscpp)
find_package( PCL REQUIRED )

catkin_package(CATKIN_DEPENDS roscpp
           INCLUDE_DIRS include
           LIBRARIES subpackage_3
           )

include_directories(
  include 
  ${catkin_INCLUDE_DIRS}
  ${PCL_INCLUDE_DIRS}
)
set(PKG3_COM_LIB_SOURCES
    src/subpackage_3.cpp)
add_library (subpackage_3 ${PKG3_COM_LIB_SOURCES})

The only modification I needed to make to subpackage_<1, 2>/CMakeLists.txt was to add find_package(subpackage_3). I also added < depend>subpackage_3< /depend> to subpackage_<1, 2>/package.xml (without the spaces).

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-05-26 16:40:01 -0500

joq gravatar image

updated 2016-05-27 09:36:31 -0500

ROS does not have a concept of "subpackage" like you seem to describe.

Perhaps the ROS equivalent would be to eliminate the src/package directory and promote each of its contained directories to that level:

 catkin_workspace/src/
                      subpackage_1/src/subpackage_1.cpp
                      subpackage_2/src/subpackage_2.cpp
                      package (Metapackage)

That gives you two ROS packages (subpackage_1 and subpackage_2) and a metapackage.

EDIT: If you like, the base class can go in its own ROS package at the same level. Then, subpackage_1 and subpackage_2 would both depend on that.

There is no way to know what CMakeLists.txt changes would be required without you editing the original question to add their current contents.

But, the ROS catkin documentation contains plenty of examples for how to do things like that:

Another alternative would be to collect everything into a single ROS package. Depending on the structure of your project that might be easier.

edit flag offensive delete link more

Comments

Okay. If I do that, where should I include the common class, and how should I modify my CMakeList.txts to reflect the changes?

mc123 gravatar image mc123  ( 2016-05-27 08:55:56 -0500 )edit

There is no way to answer that, because you have not told us what the CMakeLists.txt currently contains. Please edit your question to add that information.

joq gravatar image joq  ( 2016-06-01 21:07:26 -0500 )edit

That looks reasonable. Instead of find_package(subpackage_3) the recommended catkin usage is adding it to this: find_package(catkin REQUIRED COMPONENTS roscpp subpackage_3).

joq gravatar image joq  ( 2016-06-03 10:22:06 -0500 )edit

Also, it's a good idea to make subpackage_3 releasable, by adding the install steps: http://docs.ros.org/api/catkin/html/h... .

joq gravatar image joq  ( 2016-06-03 10:24:42 -0500 )edit

Awesome. Thanks, joq.

mc123 gravatar image mc123  ( 2016-06-03 10:36:46 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-05-26 16:09:30 -0500

Seen: 576 times

Last updated: Jun 03 '16