ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It is by now easily possible.

You create an cpp package as usual and add the package structure of a normal python package.

The difference is, that you don't need the setup.cfg and setup.py files as this is now all handled by CMakeList.txt.

You have to make small adjustments as adding

find_package(ament_cmake_python REQUIRED)
find_package(rclpy REQUIRED)

and modify the package.xml to support building with python:

<buildtool_depend>ament_cmake_python</buildtool_depend>

For more detailed explanations please look at these ressources:

ros2-package-for-both-python-and-cpp-nodes

creating-a-mixed-cpp-and-python-package

In short your structure has to look like this:

my_example_package/
CMakeLists.txt
package.xml
include/
    my_example_package/
        some_file.hpp
src/
    some_file.cpp
my_example_package/
    __init__.py
    some_module.py
    another_module.py
scripts/
    my_executable

package.xml like this:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>my_cpp_py_pkg</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="your@email.com">Name</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <buildtool_depend>ament_cmake_python</buildtool_depend>

  <depend>rclcpp</depend>
  <depend>rclpy</depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

and CMakeList.txt like this:

cmake_minimum_required(VERSION 3.5)
project(my_cpp_py_pkg)
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find dependencies
find_package(ament_cmake REQUIRED)
find_package(ament_cmake_python REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclpy REQUIRED)

# Include Cpp "include" directory
include_directories(include)

# Create Cpp executable
add_executable(cpp_executable src/cpp_node.cpp)
ament_target_dependencies(cpp_executable rclcpp)

# Install Cpp executables
install(TARGETS
  cpp_executable
  DESTINATION lib/${PROJECT_NAME}
)

# Install Python modules
ament_python_install_package(${PROJECT_NAME})

# Install Python executables
install(PROGRAMS
  scripts/py_node.py
  DESTINATION lib/${PROJECT_NAME}
)

ament_package()