简体   繁体   English

在python脚本中加载.so文件时出现未定义的符号错误

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

  1. Aren't the names resolved when a .so file is created ? 创建.so文件时,名称不解析吗?
  2. what is the best approach to resolve undefined symbols in .so file. 解决.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.

相关问题 动态加载共享库时获取未定义的符号错误 - Getting undefined symbol error while dynamic loading of shared library 在 java 中加载 c++ library.so 会引发错误:未定义符号:XOpenDisplay - loading a c++ library.so in java raises the error: undefined symbol: XOpenDisplay 在Python中加载共享库时未定义符号 - Undefined symbol when loading shared library in Python SWIG Python 未定义符号错误 - SWIG Python undefined symbol error 使用dlopen动态加载x86 android .so文件,带有未定义符号的错误 - using dlopen to dynamically load x86 android .so file, error with undefined symbol libboost_python3.so.1.56.0:未定义的符号:PyClass_Type - libboost_python3.so.1.56.0: undefined symbol: PyClass_Type ImportError:使用SWIG时_…so文件中的未定义符号 - ImportError: undefined symbol in _…so file when using SWIG 虽然符号出现在* .so文件中,但是g ++未定义引用 - g++ undefined reference although symbol is present in *.so file 带有嵌入式 Python 解释器的未定义符号错误 - Undefined Symbol Error with embedded Python interpreter 错误:文件中未定义的第一个引用符号 - Error: Undefined first referenced symbol in file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM