Get filepath of library

361 views Asked by At

I am building a library for the Libretro framework, and I want to get the path of my library so that I can open a file in the same directory as my library.so file.

I followed the instructions at How to implement readlink to find the path, but that just gives me the path of the executable that is calling my library.

1

There are 1 answers

2
Peter On

On Linux, use the Glibc extension dladdr().

#include <dlfcn.h>

std::string get_library_path() {
    Dl_info dl_info;
    if(0 != dladdr((void*)get_library_path, &dl_info))
        return std::string(dl_info.dli_fname);
    else
        return std::string();
}

On Windows, the equivalent is GetModuleFileName()