2

How can I get the application directory in C++ on Linux/Unix. getcwd() returns the directory from which the application was run, but I want the actual directory which the build is sitting in.

I know you can get this from the main() but I dont really have access to it, and I've been told you cant rely on it (Is that true?).

rbrito
  • 2,398
  • 2
  • 21
  • 24
  • Indeed, the value of `argv[0]` is not guaranteed. One could call your binary using `execv` and change the value of `argv[0]`. Don't rely on it, especially not for anything that involves security concerns. – ereOn Feb 19 '11 at 16:27
  • 2
    possible duplicate of [how to find the location of the executable in C](http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c) – ereOn Feb 19 '11 at 16:32
  • @ereOn - nothing possible about it :) –  Feb 20 '11 at 02:57
  • At least 2 people seem to agree ;) – ereOn Feb 20 '11 at 10:31

3 Answers3

2

You can see this answer:

c++ - Finding current executable's path without /proc/self/exe - Stack Overflow

Community
  • 1
  • 1
kizzx2
  • 18,775
  • 14
  • 76
  • 83
1

In general in a unix system you cannot assume that there is even a file or directory that is still pointing to the executable file. For example the file/directory could have been deleted while the program was running (this is impossible in windows but not a problem in unix/linux). On the other side there may be multiple names that are pointing to the same physical file (hard links) none of which is the principal one... what is "the name" of the file in this case?

There is no safe portable way to get the name of the program because there can be many names or no names at all that are pointing to the program. If you must find the name of the program it means you're trying to do something wrong and there's probably a better solution.

EDIT:

While there is no general portable approach for all *x systems, under linux you can use the name /proc/self/exe to open the executable file. This will work even if the file is not pointed any more by any directory (e.g. even if the file has been "deleted" while was running). The actual file (inode) of a running process will always be available if the process is still running, what may be missing is the name of the file (i.e. a directory listing that is pointing to that inode).

6502
  • 112,025
  • 15
  • 165
  • 265
  • +1 for the "you're trying to do something wrong" : on Windows, people tend to put program's resources in the same directory, but on *NIX, there is almost always a better location depending on the resource type (`/etc`, `/usr/share`, and so on) – ereOn Feb 19 '11 at 16:31
  • @ereOn I am trying to put the resources with the program, I'm making a game on PS2linux, It seems sensible enough to have the resources (music, models, textures, etc) with the binary. –  Feb 19 '11 at 16:50
  • If you need to open the actual executable file then there is `/proc/self/exe` that can be used under linux. The file (inode) of a running process will always be available... it's the name (i.e. a directory listing) that may be not existing. – 6502 Feb 19 '11 at 17:04
0

argv[0] will give you the full path to the executable - don't know if this is a general standard

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 1
    *`argv[0]` shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or `""`.* (§3.6.1), so it should depend on how the program was invoked; in my experience this tends to be quite implementation-dependent, in general you can't trust it. – Matteo Italia Feb 19 '11 at 16:16
  • Yes, IIRC if I `ln -s a b; ./b`, `argv[0]` would be `b` instead of `a`, even though `b` is just a symlink. – kizzx2 Feb 19 '11 at 16:24