C ++ How to get file name (and path) of .so executable on Unix

C ++ How to get file name (and path) of .so executable on Unix?

Something similar to GetModuleFileName on Windows.

+5
source share
3 answers

Although not a standard POSIX interface, the feature dladdr()is available on many systems, including Linux , Solaris , Darwin / Mac OS X , FreeBSD , HP-UX, and IRIX . This function takes an address, which can be a pointer to a static function in the module, for example (if applied to void *), and fills the structure with Dl_infoinformation, including the path name of the shared object containing this address (in the element dli_fname).

+6
source

, UNIX POSIX. - , $PATH , $PATH. , " installdir/bin" "installdir" " installdir/share/ name_of_program" "name_of_program". , "../share/ name_of_program/ name_of_resource_file" getenv ( "PATH" ) . , , - - .


, , QSettings Qt , ( Windows, PLIST Mac OS X, Gnome GConf Linux). ++ Project Template, Qt , , ( "--prefset", "--prefget" "--preflist" QSettings).

, XML, , " installdir/etc", " installdir/lib" - "installdir", UNIX- " installdir/lib" , . "$ XDG_CONFIG_HOME" ( ) "$ HOME/.config" ( "$ HOME" ).

$XDG_CONFIG_DIRS, ; , "/etc/xdg" "../etc/name_of_your_program.conf.xml" "$ PATH" , , "$ LD_LIBRARY_PATH", $DYLD_LIBRARY_PATH "," $DYLD_FALLBACK_LIBRARY_PATH "), " /etc/ld.so.conf ", , " /etc/ld.so.conf.d/*.conf ", , , , .

Roger XDG Basedir Spec .

+2

:

  • You can read the file /proc/{PID}/mmapfor a list of shared libraries. Where {PID} is the pid process (you can get it with getpid()).
  • Call the command line tool lddfor the program binary file (saved to argv[0]).
  • If you are writing a solution from scratch, look at the lddsource code for commands from uClibchow to get a list of shared libraries from an elf binary.
0
source

All Articles