C++ Boost.Python : 2 problems

落爺英雄遲暮 提交于 2021-02-07 07:07:16

问题


So, i search good tool to integrate my C++ code with python, and at first i looked at boost.python.

I've get hello examle from boost documentation and try to build and run it. Source code is (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 and my result :

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 similar 4 undefined errors with boost::python.

My build boost command line : bjam toolset=gcc variant=release

I found similar troubles in google (and on stackoverflow too), but didn't found answer at my case.

Problem 2 - Using module (linux)

At linux platform i've no problem with building module, 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 that? In documentation no words about module naming, quote :

can be exposed to Python by writing a Boost.Python wrapper:

#include <boost/python.hpp>

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

That's it. We're done. We can now build 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 named: libpyhello.so, but how i can use it in python iterpreter? I try import pyhello, hello_ext, libpyhello - and only with libpyhello interpreter is printed :

ImportError: dynamic module does not define init function (initlibpyhello)

All other variants of import failed with : ImportError: No module named pyhello

UPDATE 2nd question : Solved, *.so module must be named as ID used in BOOST_PYTHON_MODULE. After i change : BOOST_PYTHON_MODULE(hello_ext) to BOOST_PYTHON_MODULE(libpyhello), module is imported well as libpyhello.


回答1:


It's important that the library file is named like you declare the module here:

BOOST_PYTHON_MODULE(hello_ext)

that is hello_ext.dll or hello_ext.so.




回答2:


Hi,I have got the same problem as yours under win7 32bit with mingw, however I fixed it at last.

The possible solution is:

When build the lib boost python, use link=shared 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 link,use the BOOST_PYTHON_STATIC_LIB macro explicitly

The following is the sample 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 some lines in file 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 like this:

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

This shell will be ok, have a try.



来源:https://stackoverflow.com/questions/9140572/c-boost-python-2-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!