Application Path That Works on Both Windows/Unix

Jan 5, 2011 at 5:26am
Hi Guys!

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
Jan 5, 2011 at 9:31am
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.
Jan 5, 2011 at 9:40am
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.
Jan 5, 2011 at 9:52am
argv[0] only for windows? oh well..

And wouldn't boost::filesystem have something useful?
Jan 5, 2011 at 10:24am
No, it works elsewhere too and probably correctly. I was pointing out that on Windows, it does not return the path in all cases.
Topic archived. No new replies allowed.