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.
This is probably a very silly question but I seem to be stuck where I cannot find the solution. What is the Unix/Linux system call that gets the running executable's path?
The reason is that I want to use a particular file located in the exe's home dir, but unless I run the app from the command line from the same directory, it seems unable to locate the file.
Last edited by vharishankar; 08-02-2006 at 04:03 AM.
Wholly unsatisfactory explanation for why such a function doesn't exist. There are plenty of legitimate reasons why I would want the running executable's path. One reason is loading data files located in the same path as the executable. I know that it's not good "form" (hahaha) to store data files in the exe's path, but a program in the development stage cannot expect to have its own directory structure all laid out in advance same as every other program.
Also when people run an executable from a GUI file browser or don't execute the file from its own location, the so-called "current working directory" is not the same as the executable's location and so screws up the running of the program.
If only there were a simple way to do this. But of course UNIX gurus would say - "your program design is flawed". Bullshit excuse for not incorporating something as simple as this in a platform independent manner (i.e. works on all Unices).
Last edited by vharishankar; 08-02-2006 at 04:55 AM.
Pray how can you tell if it's flawed without seeing the code?
Surely when I write a program and want to load a data file how would I know where the final location of the data file on installation would be? And how on earth do I plan the install routine without even having a basic program in place?
Do I simply hardcode everything in and leave everything to sort itself out later?
Teach me.. since you're so full of Unix wisdom, introuble.
dmail, I have a dir structure, but this is the "project" dir structure and not the final application structure. Since the project dir would vary from the final app dir location how on earth do you guys plan on locating data files used by your programs? Currently for development I just keep the data in a subdirectory of the project dir. That's why I said I needed the path.
Surely there must be one programmer who understands what I'm getting at Do you guys never use data files in your programs?
I'm genuinely seeking help here from knowledgeable Unix programmers and how they handle this kind of a situation.
I'm only a hobbyist... not a professional for god's sake.
Last edited by vharishankar; 08-02-2006 at 05:25 AM.
Surely there must be one programmer who understands what I'm getting at Do you guys never use data files in your programs?
Of course we do.
For all of the applications I have ever created I predefine where I want the data files to be located when I perform the initial design, hard code that information into the application, and make that location a requirement for running the application.
I understand what you are trying to do, I have run into that same sort of requirement when programming a few applications for the Windows world and got around that problem by having the installer set the application's working directory into the registry. However there is no such thing like that under Linux/UNIX. I guess you could come up with something similiar in that you could have your installer script create a data file that contains the application's working directory. That data file can then be written out to a predefined location (such as the /tmp directory). Then when your application is run it can read in the data file in order to find out what directory it should use at run time.
1) you could set an environment variable and read in in your code
2) many languages pass the executable name as the fist argument of the code
3) maybe the 'which' function would do what you want
Thanks a lot rstewart and sirclif. I am using C++. As you say, I will probably have to hardcode the path for now and later (if I am going to make a distributable package) use one of the methods you suggest.
BTW,
(1) `which` is a simple shell script. So unless you want to debate whats a "Unix function call" and whats not, this is OK.
(2) On most unixes argv[0] returns only the exe minus the path. (Unlike windoze).
(3) Hari, from a purely technical view, please don't go further into "hard coding". It is better to immediately review all the code you have produced so far and make corrections ASAP. The longer you delay, the more you are committing to"hard code" type thinking and possibly one more hard coded statement. Bite the bullet. Factor in the extra time and effort. "Later" never happens.
As moderator I'm asking you to please contribute to maintaining a friendly and welcoming atmosphere at LQ for everyone by making constructive and helpful posts. If you can't manage anything else but drive-by hit-n-run oneliners then please then do not reply at all.
Thanks in advance.
//If you feel the need to discuss this you are invited to take it up with me by email.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.