Possible Duplicate:
Get path of executable
I’m programming on Windows using MinGW, gcc 4.4.3. When I use the main function like this:
int main(int argc, char* argv[]){
cout << "path is " << argv[0] << endl;
}
On Windows I get a full path like this: “C:/dev/stuff/bin/Test”. When I run the same application on Linux, however, I get some sort of relative path: “bin/Test”. It’s breaking my application! Any idea on how to make sure the path is absolute on both systems?
No, there isn’t. Under most shells on Linux,
argv[0]contains exactly what the user typed to run the binary. This allows binaries to do different things depending on what the user types.For example, a program with several different command-line commands may install the binary once, and then hard-link the various different commands to the same binary. For example, on my system:
Notice how some of these files have exactly the same size. More investigation reveals:
The inode number (459240) is identical and so these are two links to the same file on disk. When run, the binary uses the contents of
argv[0]to determine which function to execute. You can see this (sort of) in the code for Git’smain().