AC_CHECK_HEADERS: define a macro before testing for header presence

Viewed 20

A particular C++ logging library called spdlog that I use in my project has a broken package on CentOS (the platform I'm trying to compile on) where the header file will only compile if SPDLOG_FMT_EXTERNAL is defined before any of its files are included. [And this will not be fixed.]

I am trying to find a way to use autoconf directives to test for the presence of this header file - previously I used a homegrown macro that compiles a program which uses that, but its speed latency is unacceptably slow so I am trying to replace it.

Here is the relevant snippet of my configure.ac:

dnl check for libfmt is done earlier...
AC_CHECK_HEADERS([spdlog/spdlog.h], [have_spdlog="yes"], [have_spdlog="no"])
if test x$have_spdlog = xyes; then                                             
    LDFLAGS="$LDFLAGS -lspdlog -lfmt";                                  
else                                                              
    AC_MSG_ERROR([spdlog is required for logging support but is missing.])
fi

The header file spdlog/spdlog.h exists, but Autoconf deems it as not usable because it won't compile by itself.

AC_CHECK_HEADERS has a parameter I can specify include files required to include that particular header file. I can use that to specify the #define macro before the file is included.

Is there an easier way to just directly specify a macro without creating a new header file?

1 Answers

After some experimenting, I learned that the fourth parameter to AC_CHECK_HEADERS accepts macros as well - anything that can be used in a C/C++ preprocessor works as well, such as #ifdef, #endif and so on. It is not restricted to only #include directives.

AC_CHECK_HEADERS([spdlog/spdlog.h], [have_spdlog="yes"], [have_spdlog="no"], [
#define SPDLOG_FMT_EXTERNAL
])
Related