简体   繁体   中英

Undefined symbol error while loading .so file in python script

I have some C++ code, for which i have created a .so file.

My objective is to load this .so file in a python script.

In order to expose the C++ methods, i create a C wrapper method for them and expose the C method.

However, when trying to load the .so file in python script, it gives me an Undefined symbol error.

These errors are for methods that are used internally in some of the C++ classes and are not exposed out of the .so file. (nm -C -u .so)

  1. Aren't the names resolved when a .so file is created ?
  2. what is the best approach to resolve undefined symbols in .so file.

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"

You forgot to include the object ( .o file) that defines those functions when linking the .so .

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. To pass it through the g++ (or clang++ ) driver, you write it as -Wl,--no-undefined .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM