Sign in to follow this  

path to executable

This topic is 4837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently have the following problem - I have a program that requires an init file, say 'init.scm' to run. This file is in the same dir as the executable (later it might end up in a different dir, but its position relative to the executable will be fixed, say '../lib/init.scm'). Currently I am just opening this file as 'init.scm' and praying that it will work. When the program is run as './program' it is okay, but as soon as it is run from another dir ('programdir/program') it obviously can not find its init file. Is there a way to find the path to the executable in a convenient way on unix? Something that also works on win32 would be great but it appears that on windows it already normal for programs to have to be run with the 'current dir' set to the program directory. Now I could parse argv[0], but this does not always work at all (for example if the program was invoked through a link to it).

Share this post


Link to post
Share on other sites
yes, you have to read the path from /proc. here's a function which does that:

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-getexename&forum=cotd&id=-1

Share this post


Link to post
Share on other sites
Quote:
Original post by marijnh
Actually, that is Linux-specific, any other solutions that work on all Unixes?
No.
Quote:
This file is in the same dir as the executable (later it might end up in a different dir, but its position relative to the executable will be fixed, say '../lib/init.scm').
This sort of thing isn't done in the unix world.

Data is installed to specific paths, and programs know these paths at compile time.

if you take a look at automake, you'll see how programs deal with paths.

Share this post


Link to post
Share on other sites
Okay, thanks for the info C-Junkie. I suspected something like that... but then how does this work: Many unix programs that require data files do work both from the source dir right after you compile them and from /usr/local/bin after you install them. The data files are in a different place in these two situations. Does it start by looking in the current dir and look in /usr/local/lib/whatever only after that fails?

Share this post


Link to post
Share on other sites
yes, maybe. but many (esp. bigger) programs are not started directly. instead, a wrapper skript (usually a shell script) is started which sets up the parameters, either as environment variables or as command line parameters for the program.

Share this post


Link to post
Share on other sites
Don't you get the command used to invoke the program as the first argument? So if there's a 'get current directory' command, you can use a combination of that and argv[0] to deduce the executable's full path. I'm sure I've done this at some point in the past, anyway...

Share this post


Link to post
Share on other sites
Another common method is to use environment variables.

Your program checks to see if MYPROGRAM_PREFIX_DIR is set, and if so uses that, otherwise it uses the hardcoded path.

then you can just 'export MY_PROGRAM_PREFIX_DIR=/home/mine/devel/myprogram/' and run normally.

Share this post


Link to post
Share on other sites
Sign in to follow this