Skip to content

Commit a70736e

Browse files
author
Zandru
committed
Minimal example for boost::python in ros
This is a minimal ros package that uses boost::python to make a C++ function available to a python script. There are some static paths that should better be handled by cmake, though.
0 parents  commit a70736e

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

CMakeLists.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(boostpy_test)
3+
4+
## Find catkin macros and libraries
5+
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
6+
## is used, also find other catkin packages
7+
find_package(catkin REQUIRED COMPONENTS
8+
roscpp
9+
rospy
10+
)
11+
12+
## System dependencies are found with CMake's conventions
13+
find_package(Boost REQUIRED COMPONENTS python)
14+
15+
16+
## Uncomment this if the package has a setup.py. This macro ensures
17+
## modules and global scripts declared therein get installed
18+
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
19+
# catkin_python_setup()
20+
21+
22+
23+
###################################
24+
## catkin specific configuration ##
25+
###################################
26+
## The catkin_package macro generates cmake config files for your package
27+
## Declare things to be passed to dependent projects
28+
## INCLUDE_DIRS: uncomment this if you package contains header files
29+
## LIBRARIES: libraries you create in this project that dependent projects also need
30+
## CATKIN_DEPENDS: catkin_packages dependent projects also need
31+
## DEPENDS: system dependencies of this project that dependent projects also need
32+
catkin_package(
33+
# INCLUDE_DIRS include
34+
# LIBRARIES boostpy_test
35+
# CATKIN_DEPENDS roscpp rospy
36+
# DEPENDS system_lib
37+
)
38+
39+
###########
40+
## Build ##
41+
###########
42+
43+
## Specify additional locations of header files
44+
## Your package locations should be listed before other locations
45+
# include_directories(include)
46+
include_directories(
47+
${catkin_INCLUDE_DIRS}
48+
${Boost_INCLUDE_DIRS}
49+
"/usr/include/python2.7" #TODO: let cmake find the path
50+
)
51+
52+
add_library(mycpplib SHARED
53+
src/mycpplib.cpp
54+
)
55+
56+
target_link_libraries(mycpplib
57+
${catkin_LIBRARIES}
58+
${Boost_LIBRARIES}
59+
)

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Using Boost::Python with cmake
2+
==============================
3+
4+
The official boost documentation uses boosts own build tool `bjam`. To integrate it into a catkin build, I have to use
5+
cmake, though.
6+
This page looks like a very nice and detailed tutorial: https://www.preney.ca/paul/archives/107
7+
8+
Maybe even better:
9+
https://github.com/ethz-asl/Schweizer-Messer/wiki/Adding-boost::python-exports-to-your-ROS-package

package.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0"?>
2+
<package>
3+
<name>boostpy_test</name>
4+
<version>0.0.0</version>
5+
<description>The boostpy_test package</description>
6+
7+
<!-- One maintainer tag required, multiple allowed, one person per tag -->
8+
<!-- Example: -->
9+
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
10+
<maintainer email="felix@todo.todo">felix</maintainer>
11+
12+
13+
<!-- One license tag required, multiple allowed, one license per tag -->
14+
<!-- Commonly used license strings: -->
15+
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
16+
<license>TODO</license>
17+
18+
19+
<!-- Url tags are optional, but mutiple are allowed, one per tag -->
20+
<!-- Optional attribute type can be: website, bugtracker, or repository -->
21+
<!-- Example: -->
22+
<!-- <url type="website">http://wiki.ros.org/boostpy_test</url> -->
23+
24+
25+
<!-- Author tags are optional, mutiple are allowed, one per tag -->
26+
<!-- Authors do not have to be maintianers, but could be -->
27+
<!-- Example: -->
28+
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
29+
30+
31+
<!-- The *_depend tags are used to specify dependencies -->
32+
<!-- Dependencies can be catkin packages or system dependencies -->
33+
<!-- Examples: -->
34+
<!-- Use build_depend for packages you need at compile time: -->
35+
<!-- <build_depend>message_generation</build_depend> -->
36+
<!-- Use buildtool_depend for build tool packages: -->
37+
<!-- <buildtool_depend>catkin</buildtool_depend> -->
38+
<!-- Use run_depend for packages you need at runtime: -->
39+
<!-- <run_depend>message_runtime</run_depend> -->
40+
<!-- Use test_depend for packages you need only for testing: -->
41+
<!-- <test_depend>gtest</test_depend> -->
42+
<buildtool_depend>catkin</buildtool_depend>
43+
<build_depend>roscpp</build_depend>
44+
<build_depend>rospy</build_depend>
45+
<run_depend>roscpp</run_depend>
46+
<run_depend>rospy</run_depend>
47+
48+
49+
<!-- The export tag contains other, unspecified, tags -->
50+
<export>
51+
<!-- You can specify that this package is a metapackage here: -->
52+
<!-- <metapackage/> -->
53+
54+
<!-- Other tools can request additional information be placed here -->
55+
56+
</export>
57+
</package>

scripts/use_cpplib.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import rospy
5+
6+
import sys
7+
# TODO: this is ugly... can this be set using cmake?
8+
sys.path.append("/home/felix/ws/ros/devel/lib")
9+
10+
import libmycpplib as cpp
11+
12+
# this is the equivalent to `main()` in c++
13+
if __name__ == '__main__':
14+
rospy.init_node('use_cpplib')
15+
16+
cpp.hello()
17+
18+
#rospy.spin()

src/mycpplib.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include <boost/python.hpp>
3+
4+
using namespace std;
5+
6+
void hello()
7+
{
8+
cout << "Hello Python. I'm C++, looking forward to work with you!" << endl;
9+
}
10+
11+
12+
13+
BOOST_PYTHON_MODULE(libmycpplib) // name has to match with the name in CMakeLists `add_library` + prefix 'lib'
14+
{
15+
using namespace boost::python;
16+
def("hello", hello);
17+
}

0 commit comments

Comments
 (0)