Making AC_ARG_WITH affect gcc invocation for subsequent AC_CHECK_HEADERS

Viewed 295

I have a program that defaults to using the readline library (unless the user specifically disables it using --without-readline). The user can also specify an alternate location for the readline headers and library using --with-readline=, e.g., --with-readline=/usr/local.

Of course, just because the user didn't disable readline or specified an alternate location means that the readline headers and library actually exist on the system (or where the user claims they do), so I want to check for the actual presence of readline if it's not disabled.

I'm following the last example here for using AC_ARG_WITH, but then later on in my configure.ac file, I do:

AC_CHECK_HEADERS([readline/readline.h readline/history.h])

# ...

AC_SEARCH_LIBS([readline],[readline])

However, giving:

./configure --with-readline=/usr/local

results in:

checking readline/readline.h usability... no
checking readline/readline.h presence... no
checking for readline/readline.h... no
checking readline/history.h usability... no
checking readline/history.h presence... no
checking for readline/history.h... no

Looking at config.log:

configure:6517: checking readline/readline.h usability
configure:6517: gcc -std=gnu99 -std=gnu99 -c -g -O2  conftest.c >&5
conftest.c:80:31: error: readline/readline.h: No such file or directory

The invocation of gcc does not have -I/usr/local/include so of course it doesn't find it.

So the question is: how do I get configure to invoke gcc by adding -I/usr/local/include (or wherever the user specifies) when it does AC_CHECK_HEADERS and AC_SEARCH_LIBS?


I did try explicitly augmenting CFLAGS and LDFLAGS myself (as I've seen some configure.ac files do):

AC_ARG_WITH([readline],
  AS_HELP_STRING([--without-readline], [disable support for readline]),
  [],
  [with_readline=yes]
)
AS_IF([test x$with_readline = xyes],
  [
    AC_DEFINE([WITH_READLINE], [1],
      [Define to 1 if readline support is enabled.])
    CFLAGS="-I${withval} ${CFLAGS}"
    LDFLAGS="-L${withval} ${LDFLAGS}"
  ]
)

but that didn't help.


FYI:

autoconf version = 2.69
automake version = 1.16.1

1 Answers

OK, I figured it out. There were three mistakes:

  1. The line should have been:

    AS_IF([test x$with_readline != xno],
    

    If the user sets a path, then with_readline is that path, e.g., /usr/local, so it does not = xyes. Indeed, the GNU example shows it this way. (I'm not sure how I got it wrong here.)

  2. The setting of the flags should happen only when the user gives an =value, i.e., does not equal xyes:

    AS_IF([test x$withval != xyes],
      [
        CPPFLAGS="-I${withval}/include ${CPPFLAGS}"
        LDFLAGS="-L${withval}/lib ${LDFLAGS}"
      ]
    )
    
  3. As shown above, I also forgot to add the /include and /lib after {withval}.

Related