So, I'm looking for a good tool to integrate my C ++ code with python, and first I looked at boost.python.
I get hi from additional documentation and try to create and run it. Source Code (src / hello.cpp):
#include <Python.h> #include <boost/python.hpp> char const* greet() { return "hello, world"; } BOOST_PYTHON_MODULE(hello_ext) { using namespace boost::python; def("greet", greet); }
Problem 1 - Windows and mingw
I try to build my result too:
g++ -o build\hello.o -c -IE:\Programming\libs\boost_1_48_0 -IE:\Programming\Python\include src\hello.cpp g++ -shared -o pyhello.dll build\hello.o -LE:\Programming\libs\boost_1_48_0\stage\lib -LE:\Programming\Python\libs -lboost_python-mgw45-mt-1_48 -lpython27 -Wl,
Also related 4 undefined errors with boost :: python.
The command line of my build: bjam toolset=gcc variant=release
I found similar problems on google (and on stackoverflow too), but could not find an answer in my case.
Problem 2 - Using a module (linux)
On linux platform I have no problems with the build module, the same source compiled well:
g++ -o build/hello.os -c -fPIC -I/usr/include/python2.7 src/hello.cpp g++ -o libpyhello.so -shared build/hello.os -lboost_python -lpython2.7
Now how can I use this? In the documentation there are no words about module names, quote:
can be displayed in Python by writing a Boost.Python shell:
#include <boost/python.hpp> BOOST_PYTHON_MODULE(hello_ext) { using namespace boost::python; def("greet", greet); }
What is it. Were made. Now we can create this as a shared library. the resulting DLL is now visible to Python. Here's a sample Python session:
>>> import hello_ext >>> print hello_ext.greet() hello, world
So my module is called: libpyhello.so, but how can I use it in python iterpreter? I try to import pyhello, hello_ext, libpyhello - and print only with the libpyhello interpreter:
ImportError: dynamic module does not define init function (initlibpyhello)
All other import options failed: ImportError: No module named pyhello
UPDATE Second question . Solved, * .so module should be named as the identifier used in BOOST_PYTHON_MODULE. After I changed: BOOST_PYTHON_MODULE(hello_ext)
to BOOST_PYTHON_MODULE(libpyhello)
, the module is imported in the same way as libpyhello.