Using the code generated by Py ++ as a Python extension

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

I went through the tutorial for Py ++ using the tutorial files, and got the expected result in generated.cpp, but I don’t know. I did not understand what to do to actually use the generated code as an extension that I can import into Python. I'm sure I need to compile the code now, but with what? Should I use bjam ?

+5
source share
3 answers

Py ++ generates the syntax that you use with boost :: python to create python entry points in your application. Assuming everything went well with Py ++, you need to load the Boost framework and add the boost and boost :: python lib directory to your project, and then compile with the cpp created using Py ++.

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

If on windows you need to change the extension in your built-in library from .dll to.pyd. And yes, it must be a library project, it does not work with executable files.

pyd, python python [Your-library-name] , , .

, , .cpp :

BOOST_PYTHON_MODULE( -name- )

, python .

, .

, , python , , boost:: python python . , pyd , , python, . , , .

: , , python setuptools. python . - IDE , .

+6

Python ++ - sig; , , .

, , .


, , . . bjam boost. scons ( Windows Linux).

sconstruct, Py ++ unittests ( :-)):

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'
)

Python, , Py ++ "make". . Py ++ , , . , .

+3

make :

GNUmakefile:

PYTHON_INC=$(shell python-config --includes)
PYTHON_LIBS=$(shell python-config --libs)
BOOST_LIBS=-lboost_python

all:
    g++ -W -Wall $(PYTHON_INC) $(PYTHON_LIBS) $(BOOST_LIBS) -fPIC -shared generated.cpp -o hw.so

.so ipython, :

In [1]: import hw
In [2]: a = hw.animal('zebra')
In [3]: a.name()
Out[3]: 'zebra'
+1

All Articles