Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python extension building with boost

Tags:

c++

python

c

boost

I am fairly new to the boost c/c++ library. I downloaded boost library and build the library.

I created a very simple python library in c++ using boost interface (actually it is example code given in the documentation). I built it into a dll file. In the documentation it reads that this dll is exposed to python and they just show the import function in python and include the created library. I don't understand how to expose that dll to python and load the library inside in tradition ('import') manner. In case if you wanna look at the code then here it is:

#include <boost/python.hpp>

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

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

Please help I really want to build applications with c/c++ and python. I simply want to use hello_ext as:

>>>import hello_ext
>>>print hello_ext.greet()

Thank you.

like image 775
Xk0nSid Avatar asked Nov 12 '22 17:11

Xk0nSid


1 Answers

I built it into a dll file. In the documentation it reads that this dll is exposed to python and they just show the import function in python and include the created library. I don't understand how to expose that dll to python and load the library inside in tradition ('import') manner.

You need to put that shared library into the module search path. There are a few ways to achieve that.

One is:

import sys
sys.path.append("<directory-where-hello_ext-module-resides>")
import hello_ext

Your shared library should be called hello_ext.dll.

like image 158
Maxim Egorushkin Avatar answered Nov 15 '22 07:11

Maxim Egorushkin