导入错误:Boost Python示例程序
包含
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 个回答
4
7
和这里的其他帖子一样
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]
14
我通过这个链接解决了问题:“找不到这样的文件或目录”错误与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
这个方法对我有效。希望我的解释尽量简单明了,因为我为这个问题纠结了大约半个小时;)