Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Boost.Python : 2 problems
    primarykey
    data
    text
    <p>So, i search good tool to integrate my C++ code with python, and at first i looked at boost.python.</p> <p>I've get hello examle from boost documentation and try to build and run it. Source code is (src/hello.cpp):</p> <pre><code>#include &lt;Python.h&gt; #include &lt;boost/python.hpp&gt; char const* greet() { return "hello, world"; } BOOST_PYTHON_MODULE(hello_ext) { using namespace boost::python; def("greet", greet); } </code></pre> <p><strong>Problem 1 - Windows and mingw</strong></p> <p>I try to build and my result :</p> <pre><code>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' </code></pre> <p>Also similar 4 undefined errors with boost::python.</p> <p>My build boost command line : <code>bjam toolset=gcc variant=release</code></p> <p>I found similar troubles in google (and on stackoverflow too), but didn't found answer at my case.</p> <p><strong>Problem 2 - Using module (linux)</strong></p> <p>At linux platform i've no problem with building module, same source compiled well :</p> <pre><code>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 </code></pre> <p>Now, how can i use that? In documentation no words about module naming, quote :</p> <blockquote> <p>can be exposed to Python by writing a Boost.Python wrapper:</p> <pre><code>#include &lt;boost/python.hpp&gt; BOOST_PYTHON_MODULE(hello_ext) { using namespace boost::python; def("greet", greet); } </code></pre> <p>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:</p> <pre><code>&gt;&gt;&gt; import hello_ext &gt;&gt;&gt; print hello_ext.greet() hello, world </code></pre> </blockquote> <p>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 :</p> <pre><code>ImportError: dynamic module does not define init function (initlibpyhello) </code></pre> <p>All other variants of import failed with : <code>ImportError: No module named pyhello</code></p> <p><strong>UPDATE 2nd question</strong> : Solved, *.so module must be named as ID used in BOOST_PYTHON_MODULE. After i change : <code>BOOST_PYTHON_MODULE(hello_ext)</code> to <code>BOOST_PYTHON_MODULE(libpyhello)</code>, module is imported well as libpyhello.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload