[英]getting `undefined symbol` errors when trying to import shared library written in c++ in python session
I was following this tutorial from boost.python
to create a shared library.我正在按照
boost.python
中的本教程创建一个共享库。 Here's a simple code defining the methods that I want to expose to python.这是一个简单的代码,它定义了我想向 python 公开的方法。
#include <boost/python.hpp>
#include <iostream>
const int oneforth(int num, int bound) {
if (num < bound) {return num;}
return oneforth(num * (1/4), bound);
}
BOOST_PYTHON_MODULE(modd) //python module name
{
using namespace boost::python;
def("oneforth", oneforth); //python method
}
int main() {
std::cout << oneforth(10, 4);
return 0;
}
ai
I want to expose oneforth
function so I can use from modd import oneforth
.我想公开
oneforth
function 以便我可以使用from modd import oneforth
。
I'm building the shared library *.so
using -我正在构建共享库
*.so
使用 -
g++ -c -fPIC py.cpp -o py.o
g++ -shared py.so py.o
whenever I trying to import the dynamic py.so
, I get erros like undefined symbol
.每当我尝试导入动态
py.so
时,都会出现undefined symbol
之类的错误。 What am I doing wrong?我究竟做错了什么? How does one create a shared library this way?
如何以这种方式创建共享库?
I tried to reproduce this and got two different error messages 'like undefined symbol'.我试图重现这一点并得到两个不同的错误消息“如未定义的符号”。 I'll explain both since I'm not 100% sure which one you encountered.
我会解释这两个,因为我不能 100% 确定你遇到的是哪一个。
this first was:这首先是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /mnt/tmpfs/py.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv
the undefined symbol here is a mangled c++ name boost::python::objects::py_function_impl_base::max_arity() const
wich can be found in libboost_python39.so for example.这里的未定义符号是一个损坏的 c++ 名称
boost::python::objects::py_function_impl_base::max_arity() const
例如,可以在 libboost_python39.so 中找到。 This means you have to link your library with -lboost_python39
to make this symbol available.这意味着您必须将您的库与
-lboost_python39
链接以使该符号可用。
the second was:第二个是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_py)
This one went away when I gave library file the module name that is mentioned in the source code modd.so
.当我给库文件提供源代码
modd.so
中提到的模块名称时,这个就消失了。 I have never used boost_python before so I can't guarantee that this is in fact what the error means.我以前从未使用过 boost_python,所以我不能保证这实际上是错误的意思。
TL;TR TL;TR
I got it working by changing the second build line to我通过将第二条构建线更改为
g++ -shared -o modd.so py.o -lboost_python39
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.