tags:

views:

324

answers:

2

Hello World Code

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

struct World{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};

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

Compile and build ok

~/boost$ g++ -fPIC -I/usr/include/python2.6 -c hello.cpp 
~/boost$ g++ -shared hello.o -o hello.so

But when import from python side, got error.

>>> import hello.so
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ./hello.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv
>>>

Can anybody help me? Thanks in advance.

+1  A: 

Oh, I just saw this post http://stackoverflow.com/questions/1771063/help-needed-with-boost-python

and problem solved, thx

So was the solution to add "-lpython2.6 -lboost_python" to the link line? It is not completely clear to me what you learned from the other thread...
Christopher Bruns
A: 

Solved this via http://stackoverflow.com/questions/1771063/help-needed-with-boost-python

g++ -c -fPIC hello.cpp -o hello.o
g++ -shared -Wl,-soname,hello.so -o hello.so  hello.o -lpython2.6 -lboost_python

did the trick for me. I hope this is as clear as possible as i was struggling with this for about half an hour now ;)

tr9sh