LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to get absolute path of current executing C/C++ prog? (http://www.linuxquestions.org/questions/programming-9/how-to-get-absolute-path-of-current-executing-c-c-prog-371488/)

Thinking 10-10-2005 07:29 AM

how to get absolute path of current executing C/C++ prog?
 
hiho@ll

i have a binary executeable
Code:

/bin/myprog.o
then i do
Code:

cd $HOME
pwd
/root
myprog.o

what i now want is that myprog.o prints
Code:

/bin
/bin/myprog.o

so i want that myprog.o prints the absolute path to itself and the executeable file name of itself

i thought about argv[0]
but it only prints what is entered in the bash
so if you use
./myprog.o
argv[0]=./myprog.o

the function
getcwd(); would only result /root in the above example *afaik*

the filename itself should be easy
i can use argv[0] for this

but how do i get the PATH?

thx@ll

aluser 10-10-2005 10:21 AM

In linux or anywhere with a similar /proc setup, you can readlink() on /proc/self/exe . Be sure to read the man page for readlink(2) because it's not intuitive.

That's

man 2 readlink

since there's a shell command with the same name as the system call

Thinking 10-10-2005 10:27 AM

looks good!

thanks


All times are GMT -5. The time now is 12:13 AM.