Where to find files at runtime?

M

Markus Dehmann

My app reads some text files at runtime. The problem is where to find them.
In my project, we agreed to rather not define own environment variables.

But then, it's hard to know at runtime where the files are because that
changes with a "make install".

I kind of solved this by letting the Makefile generate a header file that
contains string variables with path information. The Makefile writes the
values into that header file. The only downside is that most sources have
to be recompiled for a "make install", because the header file changes with
updated values like "/usr/local/..." instead of "/home/...".

It's not really a good solution. How would you do that?

Thanks!
 
U

Ulrich Achleitner

My app reads some text files at runtime. The problem is where to find
them.
In my project, we agreed to rather not define own environment variables.

But then, it's hard to know at runtime where the files are because that
changes with a "make install".

I kind of solved this by letting the Makefile generate a header file that
contains string variables with path information. The Makefile writes the
values into that header file. The only downside is that most sources have
to be recompiled for a "make install", because the header file changes
with
updated values like "/usr/local/..." instead of "/home/...".

It's not really a good solution. How would you do that?

since the location of the executable may be anywhere, a solution is to
refer to your text files only by relative path with respect to the
location of the executable. this directory can (at least afaik) be found
in *argv[0] (from "main(argv, argc)")

should you then distribute your program, the setup must be so that the
text files go to the same realtive location with respect to the executable
then before. the simplest case of such a setup would be a zip-archive with
folder information.
 
R

Rolf Magnus

Markus said:
My app reads some text files at runtime. The problem is where to find
them. In my project, we agreed to rather not define own environment
variables.

But then, it's hard to know at runtime where the files are because that
changes with a "make install".

I kind of solved this by letting the Makefile generate a header file that
contains string variables with path information. The Makefile writes the
values into that header file. The only downside is that most sources have
to be recompiled for a "make install", because the header file changes
with updated values like "/usr/local/..." instead of "/home/...".

It's not really a good solution. How would you do that?

Write a function that returns the directory and declare only that function
in the header. This way, the header won't change and you only have to
recompile the impmementation file for that short function. Another thing
you could do is to let the program search for a configuration file in /etc
or $HOME and then read the directory from that file. You could also make
the directory configurable through a command line parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top