I just want to ask a simple question about getting the application path in C++ but will work in Unix environment. I am just a beginner so please help me on this one...
I found these during my research...
GetModuleFileName
getcwd
argv[0]
/proc/<pid>/exe
What I need to do is a function that will get the application full path, and when I compiled it in windows environment, it must also work on non-windows environment like unix.
If there is no such thing, is this possible?
#ifdef WIN32
//Call application path function in windows
#else
//Call application path function for non-windows
#endif
Path to application is in argv[0] and that should be 100% portable.
Though a program compiled on windows has no hope to work on unix. Portability means that code can be compiled on both platforms, not that a single executable could work on both.
GetModuleFileName -- Windows only, and only for the current app.
getcwd -- Get current working directory of the app, not necessarily the directory where it was started
argv[0] -- In Windows, this specifies that path used to start the app. If no path was specified, you don't get one, just the program name.
/proc/<pid>/exe -- Presumably a Linuxism, Unix doesn't necessarily have a /proc directory.
There probably isn't a single common way across all platforms that does it properly.