boost python hello程序导入错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1780003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:02:15  来源:igfitidea点击:

Import Error on boost python hello program

pythonboost

提问by user216056

include

包括

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

编译构建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.

但是当从 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
>>> 

回答by tr9sh

Solved this via "No such file or directory" error with Boost Python

通过Boost Python 的“No such file or directory”错误解决了这个问题

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

帮我解决了这个问题。我希望这尽可能清楚,因为我现在已经为此挣扎了大约半个小时;)

回答by Yu Huang

same as other post here

与此处的其他帖子相同

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

But i want to stress the importance of the position of "-lpython2.6 -lboost_python". If you put them in front of input files (hello.o), they will be somehow ignored (not linked to the final hello.so). This is at least true for g++ (Ubuntu/Linaro 4.6.3-1ubuntu5).

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

To be simple, http://ubuntuforums.org/showthread.php?t=496287suggested:

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

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

回答by user216056

Oh, I just saw this post:

哦,我刚看到这个帖子:

help needed with boost python

boost python 需要帮助

and problem solved

并解决了问题

回答by Jakobovski

I had the same issue and it turned out that I was missing a constructor on my class.

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