Build C++ Library and use in Python Package (Node)
Dear ROS(2) community,
I have developed a C++ library that exposes it's classes to python using Pybind11 bindings. As a dry-run I managed to get the python node to work with the C++ classes. Thus I am able to use this C++ library in a python node.
However, this only works if I manually build the C++ library externally and copy-paste the shared library (.so) into the ROS build folder of my python package.
Obviously this should not be the final way of doing this.
So my question is this:
- Is it possible to nest my C++ library into the python package
- Build the C++ library && Build the python package <-- in one
colcon build --symlink-install
command - Run the python package with python knowing where to look for the built library
basically, seamless integration of my C++ library in my python node
Greetings and thank you for your time,
Deniz
Asked by Deniz on 2020-10-08 06:12:45 UTC
Answers
You can't mix source code languages in one ament package. I would make two packages of it and allow ament to solve dependencies. As an alternative, build the c++ lib statically and place it in the python package as a data file.
Or, tell ament how to deal with your mixed package. You can have a look at https://github.com/colcon/colcon-meson or related for an example on how to extend colcon. But I get the feeling that this is more of a one-off type situation.
Asked by Per Edwardsson on 2022-07-26 06:03:00 UTC
Comments
It IS possible to mix C++ and Python in a single colcon
package and build by colcon
. E.g. https://answers.ros.org/question/309391
Asked by 130s on 2023-04-07 02:20:12 UTC
Here's s solution I've been working on. You'll have to tweak this pkg to your requirements. Link to pkg: https://github.com/KC-git-usr/pybind11/tree/main/ros2_ws/src/ros_answers Refence I used: https://answers.ros.org/question/362178/create-a-python-module-with-pybind11-and-use-it-in-another-package/?answer=362219#post-id-362219
Asked by kc on 2022-09-21 17:38:53 UTC
Comments
Thanks dude
Asked by chrissunny94 on 2023-08-03 01:13:32 UTC
Comments
My intuition is telling me that i should wrap this as a whole as a C++ package (and not as a python package)...
Asked by Deniz on 2020-10-08 06:26:08 UTC
If you make it a c++ package, you can't (to the best of my knowledge) make endpoints to the python code which means you can't run it via
ros2 run ...
. Similarly, with a python package you can't build the c++ code.Asked by Per Edwardsson on 2022-07-26 06:04:41 UTC