LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-10-2005, 07:29 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
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
 
Old 10-10-2005, 10:21 AM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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
 
Old 10-10-2005, 10:27 AM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
looks good!

thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Absolute path qspares Linux - Software 6 10-02-2007 03:11 AM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 07:45 AM
HTML + href absolute path does not work blizunt7 Programming 3 11-21-2005 04:59 PM
Executing commands without using the full path.. LinuxBAH *BSD 2 12-02-2003 07:16 PM
absolute path to qmail in sme 5.6? dopper Linux - Software 0 10-28-2003 01:48 PM


All times are GMT -5. The time now is 09:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration