Communicate between python and C ++

I want to create a python module that can call its functions from a C ++ class and call C ++ functions from this class

I was looking at a raise, however it doesn't seem to make any sense this applies to the shared library (which I don’t know how to create it), and I can’t use the code they use in the examples (this seems very confusing)

here is their hi-world study guide ( http://www.boost.org/doc/libs/1_55_0b1/libs/python/doc/tutorial/doc/html/index.html#python.quickstart )

Following the C / C ++ tradition, let's start with hello world. C ++ function:

char const* greet()
{
   return "hello, world";
}

can be displayed in Python by writing the Boost.Python shell:

include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

. . . DLL Python. Python:

>>> import hello_ext
>>> print hello_ext.greet()
hello, world

... Hello World ...

- , , , , , python ++.

+3
2

Python ++, , ++. , . , Python, Python.

, ++ , . , Boost.Python, , .

Python ++ , . setup.py, Setuptools Distutils, , Python . , build_ext.

, :

hello-world/
├── hello_ext.cpp
└── setup.py

setup.py:

from distutils.core import setup
from distutils.extension import Extension


hello_ext = Extension(
    'hello_ext',
    sources=['hello_ext.cpp'],
    include_dirs=['/opt/local/include'],
    libraries=['boost_python-mt'],
    library_dirs=['/opt/local/lib'])


setup(
    name='hello-world',
    version='0.1',
    ext_modules=[hello_ext])

, Python, , , , . . , , Mac OS X, Boost MacPorts.

hello_ext.cpp , , BOOST_PYTHON_MODULE , Python:

#include <boost/python.hpp>

char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

Python , :

$ python setup.py build_ext --inplace
running build_ext
building 'hello_ext' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -pipe -Os -fwrapv -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/opt/local/include -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c hello_ext.cpp -o build/temp.macosx-10.9-x86_64-2.7/hello_ext.o
/usr/bin/clang++ -bundle -undefined dynamic_lookup -L/opt/local/lib -Wl,-headerpad_max_install_names -L/opt/local/lib/db46 build/temp.macosx-10.9-x86_64-2.7/hello_ext.o -L/opt/local/lib -lboost_python-mt -o ./hello_ext.so

( --inplace Python . build, .)

hello_ext.dll ( hello_ext.so Unix) hello-world. Python , hello_ext greet, Boost.

+4

Python - . , . , a = 5, python (, , , Python), , 5, , a . , input, , , , python. .

. import, python . .py, Python . .pyd, , python , , . , , mymodule.mymethod(), , .

, , C/++ python. , python int C int, short, long. , , , ( , ), , , .. , C/ ++ , vanilla int, float, char* . python C, , , , , python. , . , , , , . . , , . , .

, Python.Boost ( ) def("greet", greet);.

+3

All Articles