You are not logged in.

#1 2012-03-28 09:07:15

veloxid
Member
Registered: 2012-03-28
Posts: 1

using c++ class in python via boost

Hey

I want to write a python prgram, which uses a c++ class, i thought it would be best with boost::python
I have my class jumoSubClientHandler which inherits from subClientHandlerL

class    jumoSubClientHandler: public subClientHandler{
public:
   jumoSubClientHandler(std::string clientName="jumoClient");
private:
   jumoInterface jumo;
public:
   void printHelp();
private:
   int extractProgramNumber(std::string data);
public:
   bool analyseData(packetData_t data);
};

to use it in python i extended it with this:

using namespace boost::python;

// Boost.python definitions to expose classes to Python
BOOST_PYTHON_MODULE(jumoSubClientHandler) {
    class_<jumoSubClientHandler> ("jumoSubClientHandler",init<std::string>())
          .def("printHelp", &jumoSubClientHandler::printHelp())
    ;
}

for testing i just defined the function printHel and the constructor.

I can compile everything with gcc and create a .o file and then an .so file
(jumoSubClientHandler.so)
but if i want to import the class in python it does not work:

>>> import jumoSubClientHandler
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initjumoSubClientHandler)
>>>

any idea what i do wrong?
best
Felix

Offline

#2 2012-03-28 15:58:42

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: using c++ class in python via boost

@veloxid: Please show the exact compiler and linker invocation. You may have forgotten to link against a required library. Besides, check that Boost.Python is linked against the same Python version you're importing the module in.

Is Boost.Python mandatory for you?  Imho there are binding tools and generators that are easier to use and more convenient (e.g. SIP or cython).

EDIT: For reference, the same question was also asked in the German Python forum.

Last edited by lunar (2012-03-28 16:06:35)

Offline

#3 2012-03-29 12:20:04

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: using c++ class in python via boost

+1 boost.python is not very easy to use 

Also worth mentioning: SWIG, which does python2 and 3, but also many other languages, and is also quite easy to use

Offline

#4 2012-04-09 01:00:13

bsilbaugh
Member
From: Maryland, USA
Registered: 2011-11-15
Posts: 141

Re: using c++ class in python via boost

IMHO I've found Boost Python much easier to use than SWIG when binding C++. Boost Python makes it possible to write nearly seamless Python/C++ code. Using Boost Python is actually quite straightforward once you've got past the initial learning curve.

Did you try implementing the examples in the Boost Python tutorial? Did those work?


- Good judgement comes from experience; experience comes from bad judgement. -- Mark Twain
- There's a remedy for everything but death. -- The wise fool, Sancho Panza
- The purpose of a system is what it does. -- Anthony Stafford Beer

Offline

#5 2012-04-09 01:25:11

Oxyd
Member
From: Czech Republic
Registered: 2008-01-17
Posts: 167

Re: using c++ class in python via boost

I'd try naming the module differently from the name of your class. There may be a clash.

Offline

Board footer

Powered by FluxBB