Skip to content

Commit e253c47

Browse files
author
Felix Widmaier
committed
Add explanation to README.md
1 parent 9a570b3 commit e253c47

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

README.md

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,66 @@
1-
Using Boost::Python with cmake
2-
==============================
1+
Using Boost::Python with catkin
2+
===============================
33

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
4+
This is a minimal example of how to use Boost::Python in a catkin package.
5+
As there is little helpful documentation about this topic on the internet, I try to summarize
6+
all necessary steps here.
77

8-
Maybe even better:
9-
https://github.com/ethz-asl/Schweizer-Messer/wiki/Adding-boost::python-exports-to-your-ROS-package
8+
First you need some C++ code. `src/mycpplib.cpp` contains a simple hello world function that is
9+
made available for Python using Boost::Python. (see documentation of Boost::Python for more
10+
details on how to use it).
1011

11-
Form http://wiki.ros.org/catkin/CMakeLists.txt:
12-
7.2 Custom output directory
13-
While the default output directory for executables and libraries is usual set to a reasonable
14-
value it must be customized in certain cases. I.e. a library containing Python bindings must be
15-
placed in a different folder to be importable in Python:
12+
To compile it and make it available for python, some configuration in the CMakeLists.txt has to
13+
be done:
1614

17-
set_target_properties(python_module_library PROPERTIES
18-
LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_PYTHON_DESTINATION}
19-
)
15+
16+
Configure cmake
17+
---------------
18+
19+
1. You need Boost::Python and the path to the python include dirs:
20+
find_package(Boost REQUIRED COMPONENTS python)
21+
find_package(PythonLibs REQUIRED) # sets ${PYTHON_INCLUDE_DIRS}
22+
Make sure to add them to the include directories:
23+
include_directories(
24+
${catkin_INCLUDE_DIRS}
25+
${Boost_INCLUDE_DIRS}
26+
${PYTHON_INCLUDE_DIRS}
27+
)
28+
2. uncomment `catkin_python_setup()`. This will set up the destination path of the python
29+
module. You also need a basic _setup.py_ in the packages root directory.
30+
3. add a library:
31+
add_library(mycpplib SHARED
32+
src/mycpplib.cpp
33+
)
34+
target_link_libraries(mycpplib
35+
${catkin_LIBRARIES}
36+
${Boost_LIBRARIES}
37+
)
38+
# change output directory, so python can find the module
39+
set_target_properties(mycpplib PROPERTIES
40+
LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_PYTHON_DESTINATION}
41+
)
42+
The last command is important because it will change the destination of the compiled library
43+
from `/catkin_ws/devel/lib/` to `/catkin_ws/devel/lib/python2.7/dist-packages/` so it can be
44+
found by python.
45+
Also don't forget the `SHARED` in `add_library`.
46+
47+
48+
Use the module
49+
--------------
50+
51+
After you compiled the package, you should be able to use the wrapped module in Python:
52+
53+
import boostpy_test.libmycpplib as cpp
54+
cpp.hello()
55+
56+
57+
References
58+
----------
59+
60+
Some references that were helpful for me in one or the other way:
61+
62+
* Boost::Python documentation: http://www.boost.org/doc/libs/1_57_0/libs/python/doc/index.html
63+
* Using Boost::Python with cmake: https://www.preney.ca/paul/archives/107
64+
* https://github.com/ethz-asl/Schweizer-Messer/wiki/Adding-boost::python-exports-to-your-ROS-package
65+
* http://wiki.ros.org/catkin/CMakeLists.txt, Section 7.2 (the `set_target_properties` command
66+
comes from there)

src/mycpplib.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ void hello()
99
}
1010

1111

12-
13-
BOOST_PYTHON_MODULE(libmycpplib) // name has to match with the name in CMakeLists `add_library` + prefix 'lib'
12+
// name has to match with the name in CMakeLists `add_library` + prefix 'lib'
13+
BOOST_PYTHON_MODULE(libmycpplib)
1414
{
1515
using namespace boost::python;
1616
def("hello", hello);

0 commit comments

Comments
 (0)