Reputation: 23
I have to write a c program, that gets a .so file (shared-object) as an argument in the main method and call a function f on it. It expects a function f to exist on this library. My program has to work for any .so file, so I can't include them directly.
so by calling
./myprogram myLibrary.so
myprogram has to do something like this:
int main(int argc, char *argv[]) {
return argv[1].f();
}
How can I achieve this and what else do I have to consider when compiling my code?
Upvotes: 2
Views: 465
Reputation: 45075
What you're trying to do is called "dynamic loading" of libraries. On Unix-like operating systems, the call you're looking for is dlopen() . It takes a filename and some flags, and opens the specified shared library. You can then use the dlsym()
routine to look up individual symbols (like your function f()), which you can then call elsewhere in your program.
Upvotes: 6