It seems to me that Linux has it easy with /proc/self/exe. But I'd like to know if there is a convenient way to find the current application's directory in C/C++ with cross-platform interfaces. I've seen some projects mucking around with argv[0], but it doesn't seem entirely reliable.
If you ever had to support, say, Mac OS X, which doesn't have /proc/, what would you have done? Use #ifdefs to isolate the platform-specific code (NSBundle, for example)? Or try to deduce the executable's path from argv[0], $PATH and whatnot, risking finding bugs in edge cases?
If you use C, you can use the getwd function:
This will you print on the standard output, the current directory of the executable.
The absolute value path of a program is in the PWD of the envp of your main function, also there's a function in C called getenv, so there's that.
Some OS-specific interfaces:
_NSGetExecutablePath()
(man 3 dyld)readlink /proc/self/exe
getexecname()
sysctl CTL_KERN KERN_PROC KERN_PROC_PATHNAME -1
readlink /proc/curproc/file
(FreeBSD doesn't have procfs by default)readlink /proc/curproc/exe
readlink /proc/curproc/file
GetModuleFileName()
withhModule
=NULL
The portable (but less reliable) method is to use
argv[0]
. Although it could be set to anything by the calling program, by convention it is set to either a path name of the executable or a name that was found using$PATH
.Some shells, including bash and ksh, set the environment variable "
_
" to the full path of the executable before it is executed. In that case you can usegetenv("_")
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.You can use argv[0] and analyze the PATH environment variable. Look at : A sample of a program that can find itself
Yes isolating platform specific code with
#ifdefs
is the conventional way this is done.Another approach would be to have a 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.Depending on the version of QNX Neutrino, there are different ways to find the full path and name of the executable file that was used to start the running process. I denote the process identifier as
<PID>
. Try the following:/proc/self/exefile
exists, then its contents are the requested information./proc/<PID>/exefile
exists, then its contents are the requested information./proc/self/as
exists, then:open()
the file.sizeof(procfs_debuginfo) + _POSIX_PATH_MAX
.devctl(fd, DCMD_PROC_MAPDEBUG_BASE,...
.procfs_debuginfo*
.path
field of theprocfs_debuginfo
structure. Warning: For some reason, sometimes, QNX omits the first slash/
of the file path. Prepend that/
when needed.3.
with the file/proc/<PID>/as
.dladdr(dlsym(RTLD_DEFAULT, "main"), &dlinfo)
wheredlinfo
is aDl_info
structure whosedli_fname
might contain the requested information.I hope this helps.