.so库导出函数不可见:_Z20符号含义及问题排查
Hey there! Let's unpack what's happening with your .so library and the missing function issue step by step.
First: What does T _Z20... mean?
Let's break down the nm output you got:
- The
Tindicates that this symbol is a global function located in the text (code) section of the library—so it is exported, just not in the format your client expects. - The
_Z20aaasPP8CashInfoPiS1_S2_S2_PcS3_part is C++ name mangling. C++ compilers do this to handle function overloading (since multiple functions can share the same name but have different parameters) and other C++ language features. The mangled string encodes both the function name and its parameter types:_Z: A standard prefix marking this as a mangled C++ symbol.20: The total length of the following function name and parameter type encoding (this number counts every character that comes after it in the symbol).aaa: Your original function name.- The trailing sequence (
sPP8CashInfoPiS1_S2_S2_PcS3_) encodes the parameter types of your function (for example,Pmeans pointer,istands forint,CashInforefers to your struct type, etc.).
Why isn't the new function visible to the client?
The core problem is a linkage mismatch between your new function and the client's lookup logic:
- Your other visible functions are almost certainly exported using C linkage, which skips name mangling. That's why their
nmoutput shows plain, human-readable function names instead of mangled strings. - Your new
aaafunction is a C++ function withoutextern "C"decoration, so the compiler applied C++ name mangling. When the client tries to look up the plain nameaaa, it can't match the mangled_Z20aaas...symbol that's actually exported.
How to fix this?
The simplest and most portable fix is to force C linkage for the function so the compiler skips name mangling. Add extern "C" to the function declaration in your library's header file:
extern "C" { // Declare your function here with its full, original signature void aaa(/* match your actual parameter types */); }
This tells the C++ compiler to export the function using C-style naming. After rebuilding the library, nm -D libmylib.so |grep " T " |grep aaa will show T aaa instead of the mangled string, and your client will be able to find it just like the other functions.
内容的提问来源于stack exchange,提问作者vico

