@@ -2,32 +2,38 @@ Using Boost::Python with catkin
2
2
===============================
3
3
4
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.
5
+ As there is little helpful documentation about this topic on the internet, I
6
+ try to summarize all necessary steps here.
7
7
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).
8
+ First you need some C++ code. ` src/mycpplib.cpp ` contains a simple hello world
9
+ function that is made available for Python using Boost::Python. (see
10
+ documentation of Boost::Python for more details on how to use it).
11
11
12
- To compile it and make it available for python, some configuration in the CMakeLists.txt has to
13
- be done:
12
+ To compile it and make it available for python, some configuration in the
13
+ CMakeLists.txt has to be done:
14
14
15
15
16
16
Configure cmake
17
17
---------------
18
18
19
19
1 . You need Boost::Python and the path to the python include dirs:
20
+
20
21
find_package(Boost REQUIRED COMPONENTS python)
21
22
find_package(PythonLibs REQUIRED) # sets ${PYTHON_INCLUDE_DIRS}
23
+
22
24
Make sure to add them to the include directories:
25
+
23
26
include_directories(
24
27
${catkin_INCLUDE_DIRS}
25
28
${Boost_INCLUDE_DIRS}
26
29
${PYTHON_INCLUDE_DIRS}
27
30
)
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.
31
+
32
+ 2 . uncomment ` catkin_python_setup() ` . This will set up the destination path of
33
+ the python module. You also need a basic _ setup.py_ in the packages root
34
+ directory.
30
35
3 . add a library:
36
+
31
37
add_library(mycpplib SHARED
32
38
src/mycpplib.cpp
33
39
)
@@ -40,45 +46,50 @@ Configure cmake
40
46
PREFIX ""
41
47
LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_PYTHON_DESTINATION}
42
48
)
43
- The last command is important because it will change the destination of the compiled library
44
- from ` /catkin_ws/devel/lib/ ` to ` /catkin_ws/devel/lib/python2.7/dist-packages/ ` so it can be
45
- found by python.
46
- Also don't forget the ` SHARED ` in ` add_library ` .
49
+
50
+ The last command is important because it will change the destination of the
51
+ compiled library from ` /catkin_ws/devel/lib/ ` to
52
+ ` /catkin_ws/devel/lib/python2.7/dist-packages/ ` so it can be found by
53
+ python. Also don't forget the ` SHARED ` in ` add_library ` .
47
54
48
55
49
56
Use the module
50
57
--------------
51
58
52
- After you compiled the package, you should be able to use the wrapped module in Python:
59
+ After you compiled the package, you should be able to use the wrapped module in
60
+ Python:
53
61
54
- import boostpy_test.libmycpplib as cpp
55
- cpp.hello()
62
+ import boostpy_test.libmycpplib as cpp
63
+ cpp.hello()
56
64
57
65
58
66
Boost::Python and ROS::NodeHandle
59
67
---------------------------------
60
68
61
- When using ROS, there is one important thing to keep in mind: Assume you have a ROS node written
62
- in Python, that uses some C++ code via Boost::Python.
63
- If the C++ code needs a ` ros::NodeHandle ` , for example to fetch some parameters from the
64
- parameter server, it will crash, because the `rospy.init_node()' does ** not** initialize roscpp!
69
+ When using ROS, there is one important thing to keep in mind: Assume you have a
70
+ ROS node written in Python, that uses some C++ code via Boost::Python. If the
71
+ C++ code needs a ` ros::NodeHandle ` , for example to fetch some parameters from
72
+ the parameter server, it will crash, because the ` rospy.init_node() ` does
73
+ ** not** initialize roscpp!
65
74
66
- To get around this, you need the _ MoveIt ROS planning interface_ which is available in the ROS
67
- repos (` sudo apt-get install ros-groovy-moveit-ros-planning-interface ` ). Once installed, add
68
- the following code to your Python node:
75
+ To get around this, you need the _ MoveIt ROS planning interface_ which is
76
+ available in the ROS repos
77
+ (` sudo apt-get install ros-groovy-moveit-ros-planning-interface ` ).
78
+ Once installed, add the following code to your Python node:
79
+
80
+ from moveit_ros_planning_interface._moveit_roscpp_initializer import roscpp_init
81
+ roscpp_init('node_name', [])
69
82
70
- from moveit_ros_planning_interface._moveit_roscpp_initializer import roscpp_init
71
- roscpp_init('node_name', [])
72
-
73
83
Now everything should work fine.
74
84
85
+
75
86
References
76
87
----------
77
88
78
89
Some references that were helpful for me in one or the other way:
79
90
80
- * This page, that I unfortunatly only found after I figured most of it out by myself, should
81
- explain everything...
91
+ * This page, that I unfortunatly only found after I figured most of it out by
92
+ myself, should explain everything...
82
93
http://wiki.ros.org/ROS/Tutorials/Using%20a%20C%2B%2B%20class%20in%20Python
83
94
* Boost::Python documentation: http://www.boost.org/doc/libs/1_57_0/libs/python/doc/index.html
84
95
* Using Boost::Python with cmake: https://www.preney.ca/paul/archives/107
0 commit comments