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!