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 6836223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:19:49+00:00 2026-05-26T23:19:49+00:00

In a C++ class, I need to call functions that come from a dynamically

  • 0

In a C++ class, I need to call functions that come from a dynamically loaded library. I get the function pointers like this:

typedef void (*TDef_libfunc)(); // also tried extern "C" typedef void (*TDef_libfunc)();

my_libfunc = (TDef_libfunc)dlsym(thelibrary, "libfunc");

(The lib function is loaded, I see it in the debugger.)

my_libfunc is declared as a member variable like this:

TDef_libfunc my_libfunc;

From within a member function of that class, I try to call my function pointer like this:

my_libfunc();

But it crashes… Am I doing this right? Is it possible to have a member variable that is a pointer to a C function?

  • 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-26T23:19:50+00:00Added an answer on May 26, 2026 at 11:19 pm

    Simple library compiled using gcc (if you compile will g++ you will need to add extern “C”).

    // test-lib.c
    // gcc -Wall -g -shared -fpic test-lib.c -o test-lib.so
    #include <stdio.h>
    
    void
    libfunc()
    {
        printf("Hello World - Message sent from the libfunc() function.\n");
    }
    

    Simple program that will load the above library (path and function hard-coded).

    I had a seg fault because I had an declared fn_ as a pointer.

    // test-loadlib.cpp
    // g++ -Wall -g test-loadlib.cpp -o test-loadlib -ldl
    #include <iostream>
    #include <dlfcn.h>
    
    typedef void (*TDef_libfunc)(void);
    
    class TestClass
    {
    public:
        TestClass() : lib_(NULL) , fn_(NULL) { }
    
        ~TestClass() { if (lib_ != NULL) dlclose(lib_); }
    
        bool
        load_library()
        {
            if ((lib_ = dlopen("./test-lib.so", RTLD_NOW)) == NULL)
                return false;
    
            // From man page, this is correct way to store function ptr.
            *(void**) (&fn_) = dlsym(lib_, "libfunc");
            if (fn_ == NULL)
            {
                dlclose(lib_);
                lib_ = NULL;
                return false;
            }
            return true;
        }
    
        void
        call_func()
        {
            if (fn_ != NULL)
                (*fn_)();
            else
                std::cout << "Function not loaded.\n";
        }
    
    private:
        void*         lib_;
        TDef_libfunc  fn_;    // Don't include '*' - it will segfault.
    };
    
    int
    main(int argc, char *argv[])
    {
        TestClass  tc;
    
        if (tc.load_library())
            tc.call_func();
        else
            std::cout << "Failed to load library.\n";
        return 0;
    }
    

    I tested and compiled this under Ubuntu 10.04 using the compiler from the repository.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to call the co-class function by reading its address from vtable of
I need a class that works like this: >>> a=Foo() >>> b=Foo() >>> c=Foo()
For a policy-based class-design I need some of the policies to call functions that
I have a vector of pointers to a class. I need to call their
I'd like to add an onload function to my custom Dojo class such that
Alright, so I have a base class which we'll call TFruit . From this
I have small class called 'Call' and I need to store these calls into
I currently have a need for a custom ListViewItem class - let's call it
I have a class that I need to binary serialize. The class contains one
I have a class that I need to perform some actions on but I

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.