J. Volkya
J. Volkya

Reputation: 1000

How to have a pointer to a C function inside a C++ class?

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?

Upvotes: 2

Views: 324

Answers (1)

Jamie Royer
Jamie Royer

Reputation: 329

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.

Upvotes: 4

Related Questions