My program needs to load some files at run time, which will be installed into whatever folder is given to ./configure --datadir=/somewhere
As my program nee
Your answer is the preferred way of doing it. The autoconf manual explains how to override various variables at "make install" time (which is very useful for packaging, for example). In doing so it says (in the section "Installation Directory Variables):
A corollary is that you should not use these variables except in
makefiles. For instance, instead of trying to evaluate `datadir' in
`configure' and hard-coding it in makefiles using e.g.,
`AC_DEFINE_UNQUOTED([DATADIR], ["$datadir"], [Data directory.])', you
should add `-DDATADIR='$(datadir)'' to your makefile's definition of
`CPPFLAGS' (`AM_CPPFLAGS' if you are also using Automake).
autotools, and build systems in general, are a convoluted business and nobody has yet come up with nice and neat ways of doing things that are general enough, which means that we have to read sections like this one and work it out fully. In any case your intuition was correct!
AC_DEFINE_UNQUOTED([DATA_PATH], ["$pkgdatadir"])
Although modifying the compiler flags is really the more usual way to do it.
In the event that you have a whole series of such paths that must be known by your source code, and you want to avoid excessive noise in your compilations (eg -DPATH1=/path/to/something -DPATH2=/path2/to/something2 -DPATH3=/path3/to/something3...ad infinitum), an alternative that may be desirable to some people is to create a new "mynewheader.h.in", with the lines
#define PATH1 "@PATH1@"
#define PATH2 "@PATH2@"
#define PATH3 "@PATH3@"
and add it to your configure.ac's AC_CONFIG_FILES line, eg:
AC_CONFIG_FILES([Makefile mynewheader.h])
Your solution is the correct one. The reason why Autoconf/Automake don't (easily) support putting the installation paths into config.h
is that you are in theory supposed to be able to override the paths at build time, like make prefix=/else/where
. This possibility is nowadays somewhat arcane, but that's the reason. (Note that this is distinct from make install prefix=/else/where/
, which is still useful, in spite of DESTDIR
.)