On Mar 6, 8:16 am, "rh00667" <rh00...@gmail.comwrote:
hi all,
i'm confused now. how i can get the full path of an application?
if myapp is in a directory which belongs to PATH, argv[0] gives me the
first token of cmd line, and not the real path of the executed
program.
so if i write anywhere:
% myapp
it runs ok from the file (for example) /usr/local/bin/myapp
but argv[0] returns me
./myapp
but i need
/usr/local/bin/myapp
thanks in advance
Of course this is platform specific but the issue is common across
platforms and any solutions would be useful across platforms. Here's
what we do (which has nothing to do with C++ even though the apps
are written in C++). Note that most of this stuff can be done in C++
but it just seems easier to use a script since one is needed for
launching the apps for other reasons.
1. Start your applications via a script and have it know specifically
where your application lives relative to where the script lives.
2. Detect the full pathname of where your script lives. Here's a
simple bash shell function which sets a variable INSDIR to the
full pathname of where the script lives. The function allows
the script to be started from any directory.
install_directory ()
{
if [ `printf '%c' $0` == "/" ]; then
INSDIR=`dirname $0`;
else
INSDIR=`pwd`"/"`dirname $0`;
fi
}
3. Launch your application with a full pathname by manipulating the
full path name of your script and the relative path of your
executable
to get the full patha name of your excutable.
Hope that helps.