|
1 |
| -Using Boost::Python with cmake |
2 |
| -============================== |
| 1 | +Using Boost::Python with catkin |
| 2 | +=============================== |
3 | 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 |
| 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. |
7 | 7 |
|
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). |
10 | 11 |
|
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: |
16 | 14 |
|
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) |
0 commit comments