Ask Your Question

Can ROS2 packages depend on ROS1 packages?

asked 2022-01-19 17:28:27 -0500

lexi gravatar image

How can I specify a dependency on a ROS1 package in a ROS2 package, so that I can include header files?

I am migrating a system from ROS1 to ROS2 and the plan is to move to hybrid system and slowly migrate nodes over time. Right now we have a shared ROS package called projectname_common. And it just includes headers for things like file paths needed in various parts of the system.

When we have a hybrid system setup, I'd like to be able to have ROS1 and ROS2 packages both depend on the common package and not have to maintain two versions.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-01-20 15:31:38 -0500

msmcconnell gravatar image

updated 2022-01-20 15:55:24 -0500

Since you mention header files, I'm going to assume you are asking about c++. Short answer is not without changing the package being depended on.

It's important to distinguish between a truly ROS1 package which uses roscpp etc. and a catkin package which uses catkin for its build process, but is otherwise not linked to ROS.

If the package is actually ROS agnostic, except the build system then its quite easy to make it compile for both ROS1 and ROS2. You need to add conditionals to your package.xml and CMakeLists.txt files


<?xml version="1.0"?>
<package format="3"> <!-- Must use package format 3 -->

   <buildtool_depend condition="$ROS_VERSION == 1">catkin</buildtool_depend>
   <buildtool_depend condition="$ROS_VERSION == 2">ament_cmake_auto</buildtool_depend>

    <build_type condition="$ROS_VERSION == 1">catkin</build_type>
    <build_type condition="$ROS_VERSION == 2">ament_cmake</build_type>


cmake_minimum_required(VERSION 3.5)

# Verify ROS installation and get ROS version
find_package(ros_environment REQUIRED)


  # Add my ROS1 build commands here

else() #ROS2

 # Add my ROS2 build instructions from ament_cmake here


NOTE: The above approach can also work for message generation if your ROS1 messages conform to the requirements of ROS2 (fields all snake_case etc.).

However, if your package uses ROS libraries such as roscpp, then creating a version that compiles in both cases becomes much more challenging and I would advise creating a separate ROS2 version and or refactoring to allow for some sort of dependency injection of the middleware functions you need. If that's not an option, then you can use precompile flags in your code which is very ugly but will get you what you want. For example this is how you could change some includes:


    #include <rclcpp/rclcpp.hpp>
    // Do some stuff

#else // ROS1

    #include <ros/ros.h>
    // Do some stuff

In that case, you would set the relevant constant in your CMakeLists.txt file with

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools



Asked: 2022-01-19 17:28:27 -0500

Seen: 33 times

Last updated: Jan 20