LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-20-2006, 04:44 AM   #1
atomas123
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Rep: Reputation: 0
how do I get the application path in c++


hi,
I know this will sound quite simple to some of you.
but I cannot seem to find the answer googling.
so if you know please answer.
 
Old 03-20-2006, 07:25 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
http://www.faqs.org/faqs/unix-faq/programmer/faq/
Quote:
1.14 How can I find a process' executable file?
===============================================

This would be a good candidate for a list of `Frequently Unanswered
Questions', because the fact of asking the question usually means that the
design of the program is flawed. :-)

You can make a `best guess' by looking at the value of `argv[0]'. If this
contains a `/', then it is probably the absolute or relative (to the
current directory at program start) path of the executable. If it does
not, then you can mimic the shell's search of the `PATH' variable, looking
for the program. However, success is not guaranteed, since it is possible
to invoke programs with arbitrary values of `argv[0]', and in any case the
executable may have been renamed or deleted since it was started.

If all you want is to be able to print an appropriate invocation name with
error messages, then the best approach is to have `main()' save the value
of `argv[0]' in a global variable for use by the entire program. While
there is no guarantee whatsoever that the value in `argv[0]' will be
meaningful, it is the best option available in most circumstances.

The most common reason people ask this question is in order to locate
configuration files with their program. This is considered to be bad form;
directories containing executables should contain *nothing* except
executables, and administrative requirements often make it desirable for
configuration files to be located on different filesystems to executables.

A less common, but more legitimate, reason to do this is to allow the
program to call `exec()' *on itself*; this is a method used (e.g. by some
versions of `sendmail') to completely reinitialise the process (e.g. if a
daemon receives a `SIGHUP').
 
Old 03-21-2006, 02:00 PM   #3
atomas123
LQ Newbie
 
Registered: Oct 2005
Posts: 13

Original Poster
Rep: Reputation: 0
hi again,
let me explain the problem I have. my app is for linux system - I am new to linux.
the reason I have to know what is the application directory is because I have to access a subdirectorry - data
where images and other files are stored.
Now here is the problem. under kdevelop if I run the app the program access the files no problem ... under KDE
when i create a launcher I have to specify what is the working directory then it works .... but under gnome I cannot
specify what is the working directory in the properties of the launcher so it does not work. the only way I can make it work is to use nautilus go to app folder and lunch it from there. or I have to specify in the app the whole application path - /home/etc ...but that path is subject to change depending on what system the app is install.

do you have any idea how I can solve this problem?
 
Old 03-22-2006, 05:13 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Arguably the most proper way to that would be to have a configure script #define the path to the bitmaps. Then "make install" will install the bitmaps into that place. And the path will be known by the program because it will be hardcoded into the program at compile time. A (small) downside of this, is that the bitmap directory will not be relocatable.

At least under Linux (i.e. it may not work with other unices) there is a another way not mentioned in the FAQ mentioned above: the symlink /etc/self/exe will point to the executable. Resolving this symlink with readlink(), will give you the path.

See these threads for example programs:
http://www.linuxquestions.org/questi...d.php?t=117965
http://www.linuxquestions.org/questi...ad.php?t=92303
http://www.linuxquestions.org/questi...d.php?t=192216
http://www.linuxquestions.org/questi...d.php?t=273614
 
Old 04-01-2009, 06:57 PM   #5
ejtttje
LQ Newbie
 
Registered: Apr 2009
Posts: 3

Rep: Reputation: 0
Hko: That's a cute trick, thanks!
(I needed the executable path to pass to addr2line via system call for automated debugging output...)

For reference though, '/etc/self/exe' should read '/proc/self/exe'
 
Old 04-01-2009, 07:51 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Hko View Post
the symlink /etc/self/exe will point to the executable. Resolving this symlink with readlink(), will give you the path.
/proc/self/exe?

I was looking into dlinfo, but it doesn't look like it's documented for Linux. Too lazy to get into the source code right now. That would also only work for dynamically-loaded executables (by calling dlopen on NULL,) and I don't know if statically-linked executables are dynamically loaded, per se.
Kevin Barry
 
Old 04-01-2009, 11:37 PM   #7
ejtttje
LQ Newbie
 
Registered: Apr 2009
Posts: 3

Rep: Reputation: 0
debugging symbols

Straying a little off topic, but I've been using dladdr()/backtrace_symbols() for getting debugging info for backtraces. You might want to note the -rdynamic gcc option, which stores all symbols in the dynamic section so they actually show up in those function calls.

But although these can give you a human-readable backtrace (especially when combined with some __cxa_demangle() action for C++), I haven't seen a way to directly extract the file and line numbers given an offset within the function... hence my popen to addr2line... although that also has trouble when the trace strays through a shared library. Looks like dlinfo might give the information needed to offset the address and call on the library instead of the executable, I haven't tried that, interesting.

(Would be nice if you could give addr2line a process id and have it handle library lookups automagically... need process id because it has to connect during execution to handle address space layout randomization. 'atos' on OS X has this functionality, but on the other hand it lacks the -rdynamic flag, so unfortunately dladdr is less useful. One step forward, one step back :-/ )
 
  


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
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 07:45 AM
C function to get application path sibtay Programming 5 06-22-2005 05:45 AM
how i migrate windows application to Linux base application vandarakalpesh Linux - Software 0 06-26-2004 03:34 AM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 06:10 PM
Path To KDE Application racing_fire Linux - Newbie 1 04-20-2003 03:20 AM

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

All times are GMT -5. The time now is 07:02 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