Finding current executable's path without /proc/self/exe

前端 未结 13 832
有刺的猬
有刺的猬 2020-11-22 01:06

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 cros

13条回答
  •  无人及你
    2020-11-22 01:33

    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.

    You cannot do that (at least on Linux)

    Since an executable could, during execution of a process running it, rename(2) its file path to a different directory (of the same file system). See also syscalls(2) and inode(7).

    On Linux, an executable could even (in principle) remove(3) itself by calling unlink(2). The Linux kernel should then keep the file allocated till no process references it anymore. With proc(5) you could do weird things (e.g. rename(2) that /proc/self/exe file, etc...)

    In other words, on Linux, the notion of a "current application's directory" does not make any sense.

    Read also Advanced Linux Programming and Operating Systems: Three Easy Pieces for more.

    Look also on OSDEV for several open source operating systems (including FreeBSD or GNU Hurd). Several of them provide an interface (API) close to POSIX ones.

    Consider using (with permission) cross-platform C++ frameworks like Qt or POCO, perhaps contributing to them by porting them to your favorite OS.

提交回复
热议问题