ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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').
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?
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.
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
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 :-/ )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.