简体   繁体   English

尝试在 python session 中导入用 c++ 编写的共享库时出现“未定义符号”错误

[英]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.

相关问题 加载Python共享库时C ++中的未定义符号 - Undefined Symbol in C++ When Loading a Python Shared Library 在Python中加载共享库时未定义符号 - Undefined symbol when loading shared library in Python Python/C++ 扩展,链接库时出现未定义符号错误 - Python/C++ Extension, Undefined symbol error when linking a library 分析用C ++为Python编写的共享库/插件? - Profiling shared library/plugins written in C++ for Python? 在Python中加载C共享库期间发生OSError(未定义符号:checkedCalloc) - OSError during loading of C shared library in Python (undefined symbol:checkedCalloc) 在Python 3.6中调用C ++扩展时,导入错误“未定义符号:_ZNK9FastNoise8GetNoiseEff” - Import error “undefined symbol: _ZNK9FastNoise8GetNoiseEff” when calling C++ extension in Python 3.6 为C库生成Python SWIG绑定时的未定义符号 - Undefined symbol when generating Python SWIG binding for C library 为 Python 构建共享库 C++ 时出现分段错误 - Segmentation Fault When Build Shared Library C++ for Python C ++ Python模块导入错误:“undefined symbol:Py_InitModule3”(Py_InitModule()) - C++ Python module import error: “undefined symbol: Py_InitModule3” ( Py_InitModule () ) Python / C ++扩展。 导入时,出现未定义的符号错误 - Python/C++ extension. On import, I get an undefined symbol error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM