Search code examples
pythonc++shared-librariesshared-objects

what's the general approach to debug the undefined symbol and potential cause?


I've been developing a Python module in C++, but I got an ImportError with the message "undefined symbol" when trying to load the shared object .so file in Python.

undefined symbol: _ZN4llvm2cl18GenericOptionValue6anchorEv

I have three questions regarding how to further debug this kind of issue:

1, is this related to the linking library issues?

2, if yes, how to figure out which library is missing or linked incorrectly?

3, are the linked library necessary to be the shared object as well? I notice that when I compiled the code, I linked some static library, i.e., with the .a suffix files.


Solution

    1. is this related to the linking library issues?

    Yes: the native library you are trying to import has not been built correctly.

    1. if yes, how to figure out which library is missing or linked incorrectly?

    The one you are trying to import.

    1. are the linked library necessary to be the shared object as well?

    I have no idea what you are asking here.

    In order to successfully import a .so library into Python, that library must have been linked with all the libraries it depends on.

    i am using g++, not clang
    I already linked the necessary (i think) llvm library.

    I suspect that your problem is either that you are linking against the wrong LLVM library(ies), or that:

    • you built LLVM libraries using Clang
    • you are trying to link these LLVM libraries into code built with g++.

    Generally that doesn't work -- you need to use the same C++ compiler for both your code and all the libraries your code depends on.


    To debug this, you need to find the symbols you depend on (_ZN4llvm2cl18GenericOptionValue6anchorEv here) and the symbols provided by the libraries you link against, e.g. nm -A libLLVM*.a | grep _ZN4llvm2cl18GenericOptionValue6anchorEv.

    I expect the second command will produce no output for you (when using the LLVM archive libraries you used to link your .so).