【问题标题】:Import Error on boost python hello programboost python hello程序上的导入错误
【发布时间】:2010-12-19 07:07:03
【问题描述】:

包括

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

编译构建ok

~/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
>>> 

【问题讨论】:

    标签: python boost


    【解决方案1】:

    通过"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
    

    为我做了诀窍。我希望这尽可能清楚,因为我已经为此苦苦挣扎了大约半个小时;)

    【讨论】:

      【解决方案2】:

      和这里的其他帖子一样

      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]
      

      【讨论】:

      • 据我了解,链接顺序敏感的原因是 GNU ld 编译时链接器是一次性链接器:它从左到右拾取要解析的符号就在命令行上,并维护一个未解析符号列表,因此如果hello.o 在末尾,它会引入新的未解析符号(在libpython2.6.solibboost_python.so 中定义),但现在不能解决,因为右侧没有定义这些符号。
      【解决方案3】:

      哦,我刚看到这个帖子:

      help needed with boost python

      问题解决了

      【讨论】:

      • 那么将“-lpython2.6 -lboost_python”添加到链接行的解决方案是什么?我并不完全清楚你从另一个线程中学到了什么......
      • @ChristopherBruns 你有没有为此找到解决方案?
      【解决方案4】:

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

      【讨论】:

      • 刚刚出现了同样的错误,这个太鬼鬼祟祟了!!非常感谢您的评论,我可能需要几天时间才能注意到这一点
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多