[Solved-5 Solutions] Finding current executable's path without proc/self/exe - linux tutorial
Problem:
How to find current executable path without proc/self/exe ?
Solution 1:
Some OS-specific interfaces:
- Mac OS X: _NSGetExecutablePath() (man 3 dyld)
- Linux: readlink /proc/self/exe
- Solaris: getexecname()
- FreeBSD: sysctl CTL_KERN KERN_PROC KERN_PROC_PATHNAME -1
- FreeBSD if it has procfs: readlink /proc/curproc/file (FreeBSD doesn't have procfs by default)
- NetBSD: readlink /proc/curproc/exe
- DragonFly BSD: readlink /proc/curproc/file
Some shells, consist of bash and ksh, set the environment variable "_" to the full path of the executable before it is executed. In that case you can use getenv("_") to get it. However this is unreliable because not all shells do this, and it could be set to anything or be left over from a parent process which did not change it before executing your program.
Solution 2:
- Check out the whereami library from Gregory Pakosz (which has just a single C file).
- To get the full path to the current executable on a variety of platforms.
- It's available as a repo on github.
Solution 3:
To using either /proc/self/exe or argv[0] is using the information passed by the ELF interpreter, made available by glibc as such:
Solution 4:
Isolating platform specific code with #ifdefs.
The other type would be to have clean #ifdef-less header wich contains function declarations and put the implementations in platform specific source files. For example, check out how Poco C++ library does something similar for their Environment class.
Solution 5:
- This code finds the executable's path in Windows, Linux, MacOS, Solaris or FreeBSD.
- To simplify the code but it's easy enough to remove.
- To use defines like _MSC_VER and __linux as the OS and compiler require.
The above version returns full paths including the executable name. If instead we want the path without the executable name, <#include boost/filesystem.hpp> and change the return statement :