How to call a function from a shared library?

Posted by Frank on Stack Overflow See other posts from Stack Overflow or by Frank
Published on 2010-03-29T13:15:16Z Indexed on 2010/03/29 13:33 UTC
Read the original article Hit count: 154

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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about dynamic-linking