[英]Undefined symbol error while loading .so file in python script
I have some C++ code, for which i have created a .so file. 我有一些C ++代码,为此我创建了一个.so文件。
My objective is to load this .so file in a python script. 我的目标是在python脚本中加载此.so文件。
In order to expose the C++ methods, i create a C wrapper method for them and expose the C method. 为了公开C ++方法,我为它们创建了一个C包装器方法并公开了C方法。
However, when trying to load the .so file in python script, it gives me an Undefined symbol error. 但是,当尝试在python脚本中加载.so文件时,它给了我一个Undefined symbol错误。
These errors are for methods that are used internally in some of the C++ classes and are not exposed out of the .so file. 这些错误是针对某些C ++类内部使用的方法,并且不会在.so文件中公开。 (nm -C -u .so) (nm -C -u .so)
Eg : 例如:
C++ :
class myclass {
public:
void func1(int _i);
int getval();
};
C wrapper :
extern "C" int getval_w();
int getval_w() {
myclass obj;
return obj.getval();
}
So, when i compile this code in a .so file and load that in a python script i get error like : Undefined symbol : "mangled name for func1" 因此,当我在.so文件中编译此代码并在python脚本中加载该代码时,我得到如下错误:未定义符号:“ func1的名称混乱”
You forgot to include the object ( .o
file) that defines those functions when linking the .so
. 链接.so
时,您忘记包含定义这些功能的对象( .o
文件)。
When linking a shared object, the linker by default does not produce error for unresolved symbols, because the symbols may be provided by another object when linking the library into the final executable. 链接共享对象时,默认情况下,链接器不会对未解析的符号产生错误,因为在将库链接到最终可执行文件时,这些符号可能由另一个对象提供。 You can explicitly tell the linker to give that error using the --no-undefined
linker flag. 您可以使用--no-undefined
链接器标志明确告诉链接器给出该错误。 To pass it through the g++
(or clang++
) driver, you write it as -Wl,--no-undefined
. 要使其通过g++
(或clang++
)驱动程序,请将其编写为-Wl,--no-undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.