Autoconf: How to get installation paths into config.h

旧城冷巷雨未停 提交于 2019-12-02 23:12:40
AC_DEFINE_UNQUOTED([DATA_PATH], ["$pkgdatadir"])

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

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

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!

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])
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!