Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoconf: How to get installation paths into config.h

Tags:

autoconf

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?

like image 409
Malvineous Avatar asked May 03 '11 08:05

Malvineous


3 Answers

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!

like image 133
markgalassi Avatar answered Oct 21 '22 08:10

markgalassi


AC_DEFINE_UNQUOTED([DATA_PATH], ["$pkgdatadir"])

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

like image 21
ptomato Avatar answered Oct 21 '22 09:10

ptomato


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

like image 24
Peter Eisentraut Avatar answered Oct 21 '22 07:10

Peter Eisentraut