LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 02-18-2002, 10:10 AM   #1
oldbee
LQ Newbie
 
Registered: Jan 2002
Posts: 20

Rep: Reputation: 0
How to get executable's full path using C/C++?


Hi, I think this question might be typical!

I have an execuatable that could be moved around in my file sys tree. I need to find out - in my C++ exe code - which dir the exe is.

I know I could use dirname/which in shell and env var passed into c code. I think they work most of the time when symbolic link is not invoved. I prefer a function in c that could tell me where the execuatble is located.

Any experience?

Thanks!

- oldbee
 
Old 02-18-2002, 04:08 PM   #2
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
Well, argv[0] contains the path that was used to run the program and getcwd() will get you the path of the current working directory which will be where the program was executed from. By munging the two together you should be able to come up with something. If the first character of argv[0] is "/", then that is the path name of the program. If it is not, then append argv[0] to the path returned from getcwd() and voila, FM. There could be some "../../" in this but it will still be a vaild path.

If the executeable was found with the PATH environment variable, the above won't find it and you will have to spin all of the paths in PATH and check each one to see if the file is there.

Out of curiosity, does the program do something based on its location in the tree?
 
Old 02-18-2002, 04:15 PM   #3
oldbee
LQ Newbie
 
Registered: Jan 2002
Posts: 20

Original Poster
Rep: Reputation: 0
To answer Malicious's question, my program does rely on where the executable is for the following info:
where the shared lib is;
where the some resource files located;

Also, I do need to think about the sym link and PATH cases.

Thanks for your answser!

oldbee
 
Old 02-18-2002, 04:51 PM   #4
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
Another pure Linux way to do this is to use the /proc file system. /proc/<pid>/exe is a symbolic link to the executeable for that pid.
 
Old 02-18-2002, 05:00 PM   #5
oldbee
LQ Newbie
 
Registered: Jan 2002
Posts: 20

Original Poster
Rep: Reputation: 0
Malicious,

Good to know that!
I probably go with the script method because I only know my pid after my shared lib is resolved, also for portability(other unix) reason I will not use /proc.

Thank you much thou.

oldbee
 
Old 02-18-2002, 10:06 PM   #6
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
This should work.
Code:
#include <stdio.h>
#include <unistd.h>

main()
{
   printf("%s\n", get_current_dir_name());
}
 
Old 02-19-2002, 09:21 AM   #7
oldbee
LQ Newbie
 
Registered: Jan 2002
Posts: 20

Original Poster
Rep: Reputation: 0
crabboy, PWD is not what I want thou. Thanks anyway.
 
  


Reply



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
full path of a file ttilt Linux - General 1 11-08-2005 06:02 PM
Need to specify the full URL path.. Why? soichih Linux - Networking 3 07-31-2004 08:37 AM
get full path of a command (in C) Hady Programming 9 11-21-2003 10:50 AM
get full path of a command (in C) Hady Linux - Software 1 11-19-2003 05:46 PM
Full Path problems chedmaster Linux - Newbie 1 06-26-2003 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:45 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration