Autoconf: How to get installation paths into config.h
Asked Answered
E

4

14

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 needs to know where this folder is at run time, I need to #define a symbol somewhere so the C code can access the path as a string.

I am currently doing this by modifying the compiler flags:

AM_CPPFLAGS = -DDATA_PATH=\"$(pkgdatadir)\"

However as the configure script already produces a config.h file with a bunch of other things in it, I would like to have the symbol appear in there instead.

Is that possible?

Examen answered 3/5, 2011 at 8:47 Comment(0)
G
6
AC_DEFINE_UNQUOTED([DATA_PATH], ["$pkgdatadir"])

Although modifying the compiler flags is really the more usual way to do it.

Guatemala answered 3/5, 2011 at 9:47 Comment(2)
Thanks! Is there any benefit by doing it the 'usual' way? I'm all for keeping to the standard, but it just seemed messier.Examen
The advantage is that you can override CPPFLAGS at configure time by doing ./configure CPPFLAGS=-DDATA_PATH=/path/to/alternate/data; doing it with config.h requires ./configure --prefix=/alternate/prefix, which gives you less control over where your various directories are.Guatemala
H
7

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!

Haematopoiesis answered 3/7, 2012 at 19:27 Comment(0)
G
6
AC_DEFINE_UNQUOTED([DATA_PATH], ["$pkgdatadir"])

Although modifying the compiler flags is really the more usual way to do it.

Guatemala answered 3/5, 2011 at 9:47 Comment(2)
Thanks! Is there any benefit by doing it the 'usual' way? I'm all for keeping to the standard, but it just seemed messier.Examen
The advantage is that you can override CPPFLAGS at configure time by doing ./configure CPPFLAGS=-DDATA_PATH=/path/to/alternate/data; doing it with config.h requires ./configure --prefix=/alternate/prefix, which gives you less control over where your various directories are.Guatemala
S
6

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.)

Syndrome answered 4/5, 2011 at 12:45 Comment(0)
I
1

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])
Izmir answered 7/6, 2016 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.