configureautotools

configure.ac proper syntax


ALL,

Im adding the following in my configure,ac:

AX_HAVE_QT()
if have_qt=yes ; then
    MY_CXXFLAGS+=@(QT_CXXFLAGS)@
    MY_LIBS+=@(QT_LIBS)@
fi
AC_SUBST(MY_CXXFLAGS)
AC_SUBST(MY_LBS)

problem is, after running autoreconf and configure the latter fails with:

../configure: line 6546: syntax error near unexpected token `('
../configure: line 6546: `        MY_CXXFLAGS+=@(QT_CXXFLAGS)@'
make: *** No targets specified and no makefile found.  Stop.

What would be the proper syntax to reference the variable in configure?

TIA!!


Solution

  • Macros in configure.ac are expanded via m4 to produce a POSIX shell script (configure). Thus, anything in configure.ac that is not a macro invocation must be in shell syntax. Additionally, output variables determined by configure are shell variables to configure itself. The @...@ syntax for interpolating them applies only to templates that are processed by configure (actually by generated script config.status). Most common among those is Makefile.in, but there can be others.

    So,

    AX_HAVE_QT()
    if [ "$have_qt" = yes ]; then
        MY_CXXFLAGS="$MY_CXXFLAGS $QT_CXXFLAGS"
        MY_LIBS="$MY_LIBS $QT_LIBS"
    fi
    AC_SUBST([MY_CXXFLAGS])
    AC_SUBST([MY_LBS])
    

    or

    AX_HAVE_QT()
    if test "$have_qt" = yes; then
        MY_CXXFLAGS="$MY_CXXFLAGS $QT_CXXFLAGS"
        MY_LIBS="$MY_LIBS $QT_LIBS"
    fi
    AC_SUBST([MY_CXXFLAGS])
    AC_SUBST([MY_LBS])
    

    However, the manual asserts that

    If an if command is not inside AC_DEFUN, and it contains calls to Autoconf macros, it should be rewritten using AS_IF.

    Your particular example does not meet those criteria, but you could use AS_IF anyway:

    AX_HAVE_QT()
    AS_IF([test "$have_qt" = yes], [
      MY_CXXFLAGS="$MY_CXXFLAGS $QT_CXXFLAGS"
      MY_LIBS="$MY_LIBS $QT_LIBS"
    ])
    AC_SUBST([MY_CXXFLAGS])
    AC_SUBST([MY_LBS])
    

    Also, the manual used to recommend that conditionals such as yours be expressed in a way that avoids either operand expanding to an empty string (even a quoted one). That is, test "x$have_qt" = xyes. You can still do that, and you will find many examples of it, but these days, your configure scripts are unlikely ever to run on a shell where it makes a difference.