usman
usman

Reputation: 1315

Setting and using path to data directory with GNU AutoTools

I am trying to use GNU AutoTools for my C++ project. I have written configure.ac, makefile.am etc. I have some files that are used by the program during execution e.g. template files, XML schema etc. So, I install/copy these files along the executable, for which I use something like:

abcdir = $(bindir)/../data/abc/
abc_DATA = ../data/knowledge/abc.cc

Now it copies the file correctly and My program installation structure looks somethings as follows:

<installation_dir>/bin/<executableFile>
<installation_dir>/data/abc/abc.cc

Now the problem is that in the source code I actually use these files (abc.cc etc.) and for that I need path of where these files resides to open them. One solution is to define (using AC_DEFINE) some variable e.g. _ABC_PATH_ that points to the path of installation but how to do that exactly?. OR is there any better way to do that. For example, in source code, I do something like:

...
ifstream input(<path-to-abc-folder> + "abc.cc"); // how to find <path-to-abc-folder>?
..

Upvotes: 3

Views: 1159

Answers (1)

thiton
thiton

Reputation: 36049

The AC_DEFINE solution is fine in principle, but requires shell-like variable expansion to take place. That is, _ABC_PATH_ would expand to "${bindir}/../data/abs", not /data/abc.

One way is to define the path via a -D flag, which is expanded by make:

myprogram_CPPFLAGS += -D_ABC_PATH='\"${abcdir}\"'

which works fine in principle, but you have to make include config.status in the dependencies of myprogram.

If you have a number of such substitution variables, you should roll out a paths.h file that is generated by automake with a rule like:

paths.h : $(srcdir)/paths.h.in config.status
     sed -e 's:@ABC_PATH@:${abcdir}:' $< > $@

As a side-note, you do know about ${prefix} and ${datarootdir} and friends, don't you? If not, better read them up; ${bindir}/.. is not necessarily equal to ${prefix} if the user did set ${exec_prefix}.

Upvotes: 5

Related Questions