Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function from a shared library?

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.

like image 675
Frank Avatar asked Mar 29 '10 13:03

Frank


People also ask

How do you call a function in another shared library?

Option 1: export all symbols from your executable. This is simple option, just when building executable, add a flag -Wl,--export-dynamic . This would make all functions available to library calls. Option 2: create an export symbol file with list of functions, and use -Wl,--dynamic-list=exported.

How do you call a function in a library?

To call a function in function library, you specify the library and function name. For an example of how to call functions in a library that uses JavaScript, refer to the example in the Load function, see Load. For IPL, use the following example which uses the following format: function_library.

How do you call a library function in C++?

We can directly call these functions in our program as per our requirements. Library functions in C++ are declared and defined in special files called “Header Files” which we can reference in our C++ programs using the “include” directive. => Visit Here For The Complete C++ Course From Experts.

How do shared libraries work?

Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn't require the code to be copied, it is done by just placing name of the library in the binary file.


1 Answers

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:

typedef std::string (*foo_t)(const std::string);
foo_t foo = NULL;

...

# ifdef _WIN32
  HMODULE hDLL = ::LoadLibrary(szMyLib);
  if (!hDll) { /*error*/ }
  foo = (foo_t)::GetProcAddress(hDLL, "foo");
# else
  void *pLib = ::dlopen(szMyLib, RTLD_LAZY);
  if (!pLib) { /*error*/ }
  foo = (foo_t)::dlsym(pLib, "foo");
# endif
  if (!foo) { /*error*/ }

  ...

  foo("bar");

  ...

# ifdef _WIN32
  ::FreeLibrary(hDLL);
# else
  ::dlclose(pLib);
# endif

You can abstract this further:

#ifdef _WIN32
#include <windows.h>
typedef HANDLE my_lib_t;
#else
#include <dlfcn.h>
typedef void* my_lib_t;
#endif

my_lib_t MyLoadLib(const char* szMyLib) {
# ifdef _WIN32
  return ::LoadLibraryA(szMyLib);
# else //_WIN32
  return ::dlopen(szMyLib, RTLD_LAZY);
# endif //_WIN32
}

void MyUnloadLib(my_lib_t hMyLib) {
# ifdef _WIN32
  return ::FreeLibrary(hMyLib);
# else //_WIN32
  return ::dlclose(hMyLib);
# endif //_WIN32
}

void* MyLoadProc(my_lib_t hMyLib, const char* szMyProc) {
# ifdef _WIN32
  return ::GetProcAddress(hMyLib, szMyProc);
# else //_WIN32
  return ::dlsym(hMyLib, szMyProc);
# endif //_WIN32
}

typedef std::string (*foo_t)(const std::string);
typedef int (*bar_t)(int);
my_lib_t hMyLib = NULL;
foo_t foo = NULL;
bar_t bar = NULL;

...

  if (!(hMyLib = ::MyLoadLib(szMyLib)) { /*error*/ }
  if (!(foo = (foo_t)::MyLoadProc(hMyLib, "foo")) { /*error*/ }
  if (!(bar = (bar_t)::MyLoadProc(hMyLib, "bar")) { /*error*/ }

  ...

  foo("bar");
  bar(7);

  ...

  ::MyUnloadLib(hMyLib);
like image 111
vladr Avatar answered Oct 08 '22 10:10

vladr