I’m trying to make simplest so library.
#include <stdio.h>
void PutLoLoLo(){
puts("Lololo");
}
compiling with g++:
g++ -shared -fPIC main2.cpp -o simple.so -Wall
and I get this in symbol table:
:$ nm -D --dynamic --defined-only simple.so
0000048c T _Z9PutLoLoLov
00002010 A __bss_start
00002010 A _edata
00002018 A _end
000004f8 T _fini
00000354 T _init
but I expect something like this:
0000048c T PutLoLoLo
00002010 A __bss_start
00002010 A _edata
00002018 A _end
000004f8 T _fini
00000354 T _init
So, of course, I get dlopen() error when I try to load it.
Please, help me: what am I doing wrong?
C++ mangles symbol names. If you want to avoid the mangling, the function must be declared as
extern C, like so:Then the link:
Will give you what you expect:
You will be able to dlopen the library and obtain the symbol via its ‘normal’ name. The function is restricted to having C semantics. So you can’t, for instance, do this with member functions, or use objects with class semantics as arguments, etc. So if you need to pass in objects, you will need to take those arguments as
void *, and cast.