Search code examples
autotoolsautoconf

Save name of found AC_SEARCH_LIBS in a custom variable


I have a repository which looks something like

configure.ac
GNUmakefile.in
src
 |--lib
 |--bin

where src/lib contains the source for shared libraries to be built, src/bin contains the source for several binaries (one of which consists of several static libraries in the same directory).

  • Problem 1: some binaries need to link with third-party libraries as well, but not all
  • Problem 2: one of these libraries is named differently on Linux and FreeBSD

I saw in this post that I can solve problem 1 by using AC_SUBST to save the name of a found library in a custom variable. But how can I save the name of the first found library (whatever it may be) when using a looping macro like AC_SEARCH_LIBS, i.e.:

AC_SEARCH_LIBS([func], [lib_name_on_linux lib_name_on freebsd], [AC_SUBST([MYLIB], [????])])

One workaround would be to have two different invocations to AC_CHECK_LIB:

AC_CHECK_LIB([lib_name_on_linux], [func], [AC_SUBST([MYLIB], ["-llib_name_on_linux"])])
AC_CHECK_LIB([lib_name_on_freebsd], [func], [AC_SUBST([MYLIB], ["-llib_name_on_freebsd"])])

This would work since only one of the above AC_CHECK_LIB would succeed, but it's ugly and it wouldn't work without additional code if I want to print an error if neither is found. What's the proper way to do that?


Solution

  • According to the documentation, AC_SEARCH_LIBS caches the resulting library name in the ac_cv_search_(function) variable. It does have two special values (no and none required) that you'll have to special-case, though. So your code might look something like this:

    AC_SEARCH_LIBS([func], [libname_linux libname_freebsd], [
      AS_IF([test x$ac_cv_search_func != "xnone required"],
        [MYLIB=$ac_cv_search_func], [MYLIB=])
      AC_SUBST([MYLIB])
    ], [
      AC_MSG_ERROR([No library found that provides func()])
    ])