Results 1 to 3 of 3
I am trying to refer to a program's path from within the program, and I am stuck. I need to open a file which should be in the same directory ...
- 08-10-2007 #1
Getting $prefix from Makefile to the C program
I am trying to refer to a program's path from within the program, and I am stuck. I need to open a file which should be in the same directory as the binary, but I am not sure how to get to the directory. As I understand it, open("filename") will only work if that happens to be my current working directory.
I am developing the program using Anjuta, so I am using AutoConf/Make. In the makefiles, they have the "$prefix" variable, which seems like the answer. I just don't know how to compile that into the program.
I hope I have described the problem clearly. Please let me know if any clarification is required. Thanks.
- 08-10-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
You can obtain the directory of your binary from argv[0], for example:
Code:#include <stdio.h> #include <libgen.h> int main(int argc, char *argv[]) { printf("Dirname : %s\n", dirname(argv[0])); return 0; }
Regards
- 08-14-2007 #3
That only works if the binary is executed directly. If I create a symlink to the binary, and run it, it will give the path to the symlink, not the binary itself.
I found an answer to my question in this thread:
http://www.linuxforums.org/forum/lin...e-install.html
So I am just going to add -DDATADIR=\""$(datadir)"\" to my CPPFLAGS, and put the file in the share directory where it belongs anyway.
Thanks for the help.


Reply With Quote