C ++ Boost.Python: 2 problems

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,--out-implib,libpyhello.a Creating library file: libpyhello.a build\hello.o:hello.cpp:(.text+0x20): undefined reference to `_imp___ZN5boost6python6detail11init_moduleEPKcPFvvE' 

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.

+6
source share
2 answers

It is important that the library file is named as you declare the module here:

 BOOST_PYTHON_MODULE(hello_ext) 

i.e. hello_ext.dll or hello_ext.so .

+4
source

Hi, I have the same problem as you have under win7 32bit with mingw , however I fixed it finally.

Possible Solution:

When creating lib-boost python, use the = shared link instead.

like:

 bjam stage toolset=gcc --with-python link=shared threading=multi runtime-link=shared variant=release,debug --user-config=user-config.jam cxxflags="-include cmath " 

When linking, use explicitly the macro BOOST_PYTHON_STATIC_LIB

The following is an example cmd line:

 g++ hello_ext.cpp -shared -O3 -DBOOST_PYTHON_STATIC_LIB -lboost_python -lpython25 -o hello_ext.pyd 

To save your time, just add a few lines to boost\python.hpp :

 #include <cmath> //fix cmath:1096:11: error: '::hypot' has not been declared #if defined(__MINGW32__) && defined(_WIN32) #if !defined(BOOST_PYTHON_SOURCE) #define BOOST_PYTHON_STATIC_LIB #endif #endif ... here,other includes files ... 

Then you can simply use cmd as follows:

 g++ hello_ext.cpp -shared -lboost_python -lpython25 -o hello_ext.pyd 

This shell will be ok try.

+3
source

Source: https://habr.com/ru/post/907642/


All Articles