You compiled a C program with the C++ compiler g++
(and your small C code happens to be valid C++ code). So you observe some name mangling. You should use gcc
instead (it is part of the same compiler suite GCC....):
gcc -Wall -g -O -fPIC -shared lib.c -o lib.so
(I strongly recommend compiling with -Wall
to get all warnings, and with -g
to get debug information; the optimize flag -O
is optional -it could be -O2
to optimize more- and might improve performance of the generated plugin)
If you want to use C++ with dlopen
& dlsym
be sure to read the C++ dlopen mini-howto.
Don't forget to read documentation about Invoking GCC
If you want to avoid name mangling in genuine C++ code, declare your functions as extern "C"
.