ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org
Ask Your Question
1

How to use Python setuptools to build a Python extension

asked 2011-04-14 01:22:12 -0600

AndrewStraw gravatar image

updated 2014-01-28 17:09:32 -0600

ngrennan gravatar image

I'd like to do some heavy lifting in C++ but write most of the high-level logic for my package in Python. Consequently, I'd like to have a ROS package in which the build step compiles a C++ source code file under the direction of Python distutils. (This is generally the right way to compile C/C++ code so that all the configuration options are such that the same options used to compile Python are used for the extension module.)

Is there an example somewhere in the ROS world of building a Python extension using distutils (and consequently setup.py) under the direction of the standard ROS CMake packaging stuff?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2011-04-26 21:41:01 -0600

AndrewStraw gravatar image

Thanks. I'll have a look at the packages you mentioned. However, I was under the impression that one really wants to compile Python extensions (shared object files that link symbols from the Python interpreter and are loaded in Python as modules) with distutils so that various C runtime parameters are set correctly. I don't recall the details but could imagine that one essentially wants to include the config.h that corresponds to the build of Python in use. (Of course, if that's all that's necessary, that would be easy enough to specify another way and then use of setup.py/distutils wouldn't be necessary.)

That being said, I did manage to get rosmake to build my extension using setup.py, and it Works For Me. In my case, I am compiling an extension named "pyosgvr", which has symbols from both the Cython source file pyosgvr.pyx and the C++ source file osgvr.cpp. The relevant lines in my CMakeLists.txt:

# use distutils to build the extension module
SET(PYTHON_EXECUTABLE python)
add_custom_target(pyosgvr.so ALL
                COMMAND ${PYTHON_EXECUTABLE} setup.py build_ext --inplace
                DEPENDS src/pyosgvr.pyx src/osgvr.cpp
                WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        )

# move the built extension module into the correct location (this is a total hack)
add_custom_target(pyosgvr ALL
                COMMAND cp ${CMAKE_SOURCE_DIR}/pyosgvr.so src/vros_projectors/
                WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        )

# make sure the above commands run in the correct order
add_dependencies(pyosgvr pyosgvr.so)
edit flag offensive delete link more
1

answered 2011-04-15 10:05:53 -0600

tfoote gravatar image

We use python wrappers in several packages already. See tf and kdl for two examples.

We don't compile them using distutils as they're general c++ libraries being used from inside python.

If you want to use distutils all you have to do is write the Makefile in your package to call distutils as needed to build the objects in your package.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-04-14 01:22:12 -0600

Seen: 2,323 times

Last updated: Apr 26 '11