0

How can a shared library (.so) implementer ascertain the location of the shared library .so file in the filesystem from within the implementation?

IOW, if I am writing shared library foo.so in the C programming language, how can I get a pointer to a character string which contains the full path on the Linux file system to where foo.so lives?

rohitsan
  • 1,001
  • 8
  • 31
  • 1
    Why would you need this? I suspect there's not an easy way to do it. – Barmar Nov 17 '17 at 21:02
  • I have a DLL that needs to interrogate other DLLs in its own folder to extract some information out of them. This is possible in Windows using GetModuleFileName. – rohitsan Nov 17 '17 at 21:11
  • 1
    @rohitsan There is nothing like GetModuleFileName for shared libraries on linux. The convention is to hard-code the full path to where the library gets installed(or a directory for where to iterate through and look for plugins), or read the path of such a folder from a config file. Bar that, you can (or someone can answer this question with an robust implementation) perhaps parse and extract the location of your library by reading the "file" `/proc/self/maps` – nos Nov 17 '17 at 21:54
  • Have a look at [How to set the environmental variable LD_LIBRARY_PATH in linux](https://stackoverflow.com/questions/13428910/how-to-set-the-environmental-variable-ld-library-path-in-linux) and look at the `-rpath` linker option. – David C. Rankin Nov 18 '17 at 01:33

0 Answers0