autotoolsautomake

How to detect in autotools what libc uses Linux distributive: glibc, uclibc, musl, etc


There are different implementation of some (for example backtrace) function in code. This implementation differs depending on libc version. In my case it could be glibc (Debian) or musl libc (openwrt). How to detect what libc is installed in Linux distro?

Using Autotools for a project with platform specific source code only detects CPU platform not libc version


Solution

  • How to detect what libc is installed in Linux distro?

    The Autotools have no built-in mechanism for identifying the C library implementation, and I don't know any good, portable way to programmatically make such a determination in general.

    But it seems that what you really want to know is whether certain functions exist and have behavior satisfying a particular criterion. It is best and most idiomatic to test such things directly, rather than relying on proxies such as CPU architecture and standard library family. In this case, it's probably easier, too. Among the benefits are that

    You raised backtrace() as an example. In this case, you could generate a simple test program that calls that function, checks its output, and indicates via its exit status whether it meets your requirement. You could first check for its existence first with AC_CHECK_FUNC and / or check for the existence of its header with AC_CHECK_HEADER, but that's probably unnecessary because the tests those will perform are a subset of what you want to follow up with. Specifically, you're probably looking for AC_RUN_IFELSE. For example,

    AC_MSG_CHECKING([[whether backtrace() exists and works]])
    AC_RUN_IFELSE([
      AC_LANG_PROGRAM([[
        #include <execinfo.h>
      ]], [[
        void *buffer[2];
        exit(!(1 == backtrace(buffer, 2)));
      ]])
    ], [
      AC_MSG_RESULT([yes])
      # shell code to execute in this case (possibly including other Autoconf macros), maybe:
      backtrace_works=1
    ], [
      AC_MSG_RESULT([no])
      # shell code to execute in this case (possibly including other Autoconf macros), maybe:
      backtrace_works=0
    ])
    

    You could put such code directly in your configure.ac, or wrap it in an external macro definition and put just an invocation of that macro in configure.ac.