Check programmatically where an executable is located?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Check programmatically where an executable is located?
Hi All,
In linux, when you put an executable in the user's search path, and then when the user types the executable's name at the console, somehow the system finds out where the executable is located and executes it. My question is: is there anyway programmatically inside the executable file itself to know where it is located? To make it clear, say for example, you have an executable called "test", and you put this file in /usr/bin, then what I want is that during run time, test knows itself resides in /usr/bin.
The reason I ask this question is that I want to put some other accessory files for the executable together in the same directory, but the executable needs to know where itself is located. Somewhat irony! Is there any other way to deal with this issue?
Most programs know where they're installed because of configure, but that path has no meaning to the program in most situations under Linux. Data files and the like are installed to different directories. If you absolutely need that script to run in that directory, wrap it with a shell script.
One file I was thinking that needed to be put in the same directory as the executable is a logo file, basically a .xpm file. The executable has to know where the logo file is in order to load the logo automatically.
With configure, how do the programs know where they are installed since those information has to be stored in a file most likely, but the program needs to know where to locate that file? Sorry for my ignorance.
A program can do getpid() to get its own process ID, and if you wanted to, you could then have it use a call to the procfs to get its "exe" name (full pathname). Pick a process on your machine and do ls -l /proc/<pid>/exe to see what I mean.
Your method worked great, by the way, how portable is this method among different linux and unix?
Quote:
Originally posted by PaulFrields A program can do getpid() to get its own process ID, and if you wanted to, you could then have it use a call to the procfs to get its "exe" name (full pathname). Pick a process on your machine and do ls -l /proc/<pid>/exe to see what I mean.
Not sure about other UNIX... I would suspect getpid() is fairly portable, since it conforms to POSIX, BSD 4.3, etc.... see man page for more info. Procfs is a Linux animal though. Seems to me you should be able to Google for more info.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.