Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 688799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:16:00+00:00 2026-05-14T02:16:00+00:00

What is the easiest and safest way to call a function from a shared

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:16:00+00:00Added an answer on May 14, 2026 at 2:16 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the easiest way to separate the directory name from the file
What's the easiest way to convert a percentage to a color ranging from Green
What is the easiest way to merge XML from two distinct DOM Documents? Is
What is the easiest and safest way to retrieve XmlHttpRequest object that works across
Easiest way to explain what I mean is with a code sample. This doesn't
The easiest way to think of my question is to think of a single,
In TFS whats the easiest way of linking a backlog item to a large
Whats the best/easiest way to obtain a count of items within an IEnumerable collection
What would be the easiest way to be able to send and receive raw
What would be the easiest way to detach a specific JPA Entity Bean that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.