What is the easiest and safest way to call a function from a shared library / dll? I am mostly interested in doing this on linux, but it would be better if there were a platform-independent way.
Could someone provide example code to show how to make the following work, where the user has compiled his own version of foo
into a shared library?
// function prototype, implementation loaded at runtime:
std::string foo(const std::string);
int main(int argc, char** argv) {
LoadLibrary(argv[1]); // loads library implementing foo
std::cout << "Result: " << foo("test");
return 0;
}
BTW, I know how to compile the shared lib (foo.so
), I just need to know an easy way to load it at runtime.
On Linux you need to use dlsym. See an example at the end of the page. On Window: GetProcAddress.
NOTE: You are passing C++ objects (in this case STL strings) around library calls. There is no standard C++ ABI at this level, so either try to avoid passing C++ objects around, or ensure that both your library and your program have been built with the same compiler (ideally the same compiler on the same machine, to avoid any subtle configuration-related surprises.)
Do not forget to declare your exported methods
extern "C"
inside your library code.The above having been said, here is some code implementing what you said you want to achieve:
You can abstract this further:
LoadLibrary is a Windows function for loading DLLs. You can check for symbol existence with GetProcAddress. On Linux/Unix you want dlopen/dlsym. To do this in cross platform, you could write a function that calls either of these methods using pre-processor, so, something like:
This is one way to achieve this sort of thing. You could also do it by including a different header in your own source tree for specific platform implementations of functions. This is probably a better way. In either case the idea is to abstract from the underlying API.