在boost python hello程序上导入错误

时间:2009-11-22 21:06:59

标签: python boost

包括

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)
    ;
}

编译并构建好

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

但是当从python方面导入时,出错了。

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

任何人都可以帮助我吗?提前谢谢。

4 个答案:

答案 0 :(得分:13)

通过"No such file or directory" error 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

为我做了诀窍。我希望这一点尽可能清楚,因为我现在正在努力争取半个小时;)

答案 1 :(得分:4)

哦,我刚刚看到这篇文章:

  

help needed with boost python

问题解决了

答案 2 :(得分:3)

与其他帖子相同

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

但我想强调“-lpython2.6 -lboost_python”位置的重要性。如果你把它们放在输入文件(hello.o)的前面,它们将被忽略(没有链接到最后的hello.so)。对于g ++(Ubuntu / Linaro 4.6.3-1ubuntu5)来说,这至少是正确的。

简单来说,http://ubuntuforums.org/showthread.php?t=496287建议:

  g++ <.cpp or .o file(s)> [LDFLAGS] [LIBS] -o [appname]

答案 3 :(得分:1)

我遇到了同样的问题,结果发现我在班上缺少一个构造函数。