3

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.

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"

2
  • Even if you expose the symbols correctly, you won't be able to run this function from within Python. You need to follow the Python extension guides: docs.python.org/2/extending Commented Nov 1, 2012 at 9:34
  • 2
    I am calling function exposed from .so in python script. Don't think all that is required, as all worked fine in my sample test code. Commented Nov 1, 2012 at 9:40

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, my mistake.. found the object file that was missing in the make file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.