Reputation: 1337
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)
E.g. :
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"
Upvotes: 3
Views: 7359
Reputation: 76296
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
.
Upvotes: 2