Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Python and C++ header files

I want to provide python interface to my C++ shared library, I am planning to use Boost::python for the same, my C++ code based is huge and divided across headers and implementation files. But all the python::boost examples talks about adding python::boost constructs in cpp files and how to deal this for header files. and Can I have same code based so that I can build both C++ shared library and python modules

like image 339
Avinash Avatar asked Oct 23 '25 17:10

Avinash


1 Answers

Here is a link to a guide on how to use Boost.Python: http://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html

Example using header / implementation structure:

//hpp
#include <boost/python.hpp>
using namespace boost::python;

struct World
{
    void set(std::string msg);
    std::string greet();
    std::string msg;
};

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
}

//cpp

void World::set(std::string msg) { this->msg = msg; }
std::string greet() { return this->msg; }
like image 114
Robin Avatar answered Oct 26 '25 06:10

Robin