I''m writing a shared library (aka "shared object" or "DLL" on Windows). I have been able to compile the library successfully and link to it in my application and run the application successfully.
The problem I am having is that gcc is mangling the names of my functions in the library. This is preventing me from programmatically loading the functions at run time.
Here is some example code. This first section is a test library. In this case, it is very simplistic, just to get the idea.
testlib.cpp
#include <stdio.h>
void printNumber (int number){
printf ("The number you want to print is \"%d\".\n", number);
return;
}
I compile the source into an object file with the following command:
g++ -fPIC -c testlib.cpp
Then I compile the shared object with the following command:
g++ -shared -fPIC testlib.o -o libtestlib.so
Now, I have a viable shared object. If I create a header file with the function prototype (void printNumber (number)), then I can link a program to this library and run it like normal.
For example:
#include "testlib.h"
int main () {
printNumber (5);
return 0;
}
This program will print:
The number you want to print is "5".
But that is not what I want to do. I want to be able to load my library programmatically at runtime. The following code demonstrates what I want to do:
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main () {
void *handle;
void (*print_num)(int);
char *error;
// Attempt to open the library
handle = dlopen ("libm.so", RTLD_LAZY);
// Error checking is omitted for clarity.
// Load the cos function from the library.
print_num = (void(*)(int))dlsym (handle, "printNumber");
// If printNumber is not defined, print an error message here
// Use the function
(*print_num)(5);
dlclose (handle);
return 0;
}
I compile this program with the following command:
g++ -rdynamic -ldl main.cpp -o testProg
It compiles fine (no errors or warning). But when I run the program, I get an error:
quote:
./testProg: undefined symbol: printNumber
The reason printNumber is undefined is due to the compiler''s name mangling. When I looked at libtestlib.so in a hex editor, all occurrences of printNumber were _Z11printNumberi. When I looked at some of the standard c and c++ libraries (libm.so, libc.so, etc), the names of the functions were not mangled. So my question is how do I create my shared object so it doesn''t mangle the names? Thank you for your patience with my long windedness and for your help