2

I'd like my application to have portable access to the configuration files installed during make install (dist_sysconf_DATA). Is it possible to access $(sysconfdir) via config.h?

ben lemasurier
  • 2,582
  • 4
  • 22
  • 37

3 Answers3

3

It is, but you should not do this according to official voices (as in, I am not gonna search the manual for it now) so as to continue supporting overriding it for specific objects to be built.

make CPPFLAGS="-USYSCONFDIR -DSYSCONFDIR=/blah" thisoneobject.o

Hence, what one is supposed to do:

AM_CPPFLAGS = -DSYSCONFDIR=\"${sysconfdir}\"
jørgensen
  • 10,149
  • 2
  • 20
  • 27
  • I may not have been clear enough, I'm not trying to force the location, just have access to what is was set to at installation. Much like `PACKAGE_VERSION` is available via config.h – ben lemasurier Jan 27 '12 at 18:45
  • I think it's this section: http://www.gnu.org/software/automake/manual/html_node/Flag-Variables-Ordering.html#Flag-Variables-Ordering – Jack Kelly Jan 27 '12 at 20:11
  • 1
    @JackKelly: No, it's autoconf.info section 4.8.2 Installation Directory Variables: “*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)'`.*” – jørgensen Jan 27 '12 at 21:36
  • 1
    -D flags belong in CPPFLAGS, not CFLAGS – William Pursell Feb 01 '12 at 02:09
1

If you're using autoheader, adding this to your configure.ac will output a SYSCONFDIR macro in your config.h, and it will be defined with the value $(sysconfdir) or ${prefix}/etc.

if test "x$sysconfdir" = 'x${prefix}/etc'; then
    if test "x$prefix" = 'xNONE'; then
        sysconfdir=$ac_default_prefix/etc
    else
        sysconfdir=$prefix/etc
    fi
fi

AC_DEFINE_UNQUOTED([SYSCONFDIR], ["$sysconfdir"], [location of system configuration directory])

But I would strongly recommend against doing that, and instead, stick with using the -DSYSCONFDIR flag. It's less code and therefore less prone to something going wrong. Using a condition in configure.ac such I mentioned may not be portable or take into account every case that might be encountered. Using -DSYSCONFDIR is the best option. Sometimes appearance just doesn't matter.

Andy Alt
  • 297
  • 2
  • 12
0

What I believe is most commonly done (and this is what I do)

Add the following in your Makefile.am

AM_CPPFLAGS = -DSYSCONFIR='"$(sysconfdir)"'

And now you can access SYSCONFDIR in source

ManuPK
  • 11,623
  • 10
  • 57
  • 76
Victor
  • 1