Getting address of non-static member function for locating its shared library
16:22 03 Dec 2025

I have a situation similar to described in a blog post: a visible inline member function is called from several shared libraries which are built potentially with different compilers or compiler options. And I would like to known for a function, which shared library it is taken from. I known that on Linux int dladdr(void *addr, Dl_info *info); from can give me the answer.

For global functions and static member functions is easy to get void *adder using reinterpret_cast, but how to get it for a non-static member function, whose pointer cannot be simply casted to void*? For example:

#include 
#include 

struct __attribute__((visibility("default"))) A {
    static void f() {}
    void g() {}
};

void* fAddr = reinterpret_cast( &A::f );
//void* gAddr = ??? ( &A::g );

int main() {
    Dl_info info;
    dladdr(fAddr, &info);
    std::cout << info.dli_fname; // prints shared library name
    //same for gAddr
}

Godbolt

What can replace gAddr = ??? ( &A::g ) in the code to call dladdr(gAddr, &info)?

c++ casting shared-libraries pointer-to-member dladdr