views:

494

answers:

2

I have a need to wrap an existing C++ library for use in Python. After reading through this answer on choosing an appropriate method to wrap C++ for use in Python, I decided to go with Py++.

I walked through the tutorial for Py++, using the tutorial files, and I got the expected output in generated.cpp, but I haven't figured out what to do in order to actually use the generated code as an extension I can import in Python. I'm sure I have to compile the code, now, but with what? Am I supposed to use bjam?

+3  A: 

Py++ generates you syntax you use along with boost::python to generate python entry points in your app. Assuming everything went well with Py++ you need to download the Boost framework, and add the boost include directory and the boost::python lib to your project then compile with the Py++ generated cpp.

You can use whatever build system you want for your project, but boost is built with bjam. You need to choose whether you want a static lib or a dynamic boost python lib then follow the instructions for building boost here.

If on windows, you need to change the extension on your built library from .dll to.pyd. And yes it needs to be a library project, this does not work with executables.

Then, place the pyd where the python on your machine can find it and go into python and execute import [Your-library-name] and hopefully everything will work.

One final note, the name given in generated.cpp in this macro:

BOOST_PYTHON_MODULE( -name- )

needs to be the exact name of your project, otherwise python will complain.

I just went through all this less than a month ago so I know about the confusion.

One thing I did to make my python extension very easy to use while building the library and testing, was to build boost::python and python myself in my build environment. That way the pyd ends up exactly where I want it and users do not need to install python in order to run with my extension. That may be overkill for what you are doing though.

Edit: If you want your extension to be easily installed and compiled on a machine, check out python's setuptools. With just a few simple lines you can have python compile and install your package for you. One downside though is its not IDE compatible for those of us who like developing in visual studio.

Charles
A: 

The following answer was provided to me by Roman Yakovenko on the Python C++-sig mailing list; I'm posting it here, with minor edits, for the benefit of the Stack Overflow community.

I don't fully comprehend the answer yet, but I felt it points me in the right direction.


After you have generated the code, you have to compile it. For this purpose, you can use your favorite build system. I use bjam only to compile boost. After this, I prefer to use scons (on Windows and on Linux).

The following is an example of sconstruct file, which is used to compile one of the Py++ unittests (this is generated code too :-) ):

import sys
env = Environment()

if 'linux' not in sys.platform:
   env['MSVS'] = {'VERSION': ''}
   env['MSVS_VERSION'] = ''
   Tool('msvc')(env)

t = env.SharedLibrary(
    target=r'abstract_classes',
    source=[r'/home/roman/language-binding/sources/pyplusplus_dev/unittests/temp/abstract_classes.cpp'],
    LIBS=[r"boost_python"],
    LIBPATH=[r"", r"/home/roman/include/libs"],
    CPPPATH=[
        r"/home/roman/boost_svn",
        r"/usr/include/python2.6",
        r"/home/roman/language-binding/sources/pyplusplus_dev/unittests/temp",
        r"/home/roman/language-binding/sources/pyplusplus_dev/unittests/data",
        r"/home/roman/boost_svn"
    ],
    CCFLAGS=[  ],
    SHLIBPREFIX='',
    SHLIBSUFFIX='.so'
)

Since your code generator written in Python, you can continue where Py++ stops and generate your favorite "make" file. You can go even father. Py++ tests generate the code, compile, load the new module and test the functionality. All this is done in a single, stand alone process.

gotgenes