version script, linker, autoconf check, and the like

Here's a short configure.ac example with an autoconf test to check whether the linker supports version scripts.

I spent a few minutes searching for such a check and no results popped up, so I resorted to writing my own.

The code below might be useful for others looking for such a code snippet, and can be easily adapted to suit specific needs.

AC_INIT(--version-script test example, 0.1, matteo.vescovi@yahoo.co.uk)

AC_PROG_CC
AC_PROG_CPP

AC_MSG_CHECKING([whether linker supports version scripts])
cat > conftest.map <<EOF
VERSION_1 {
global:
        main;
local:
        *;
};

VERSION_2 {
global:
        main;
        hello_world;
} VERSION_1;
EOF

save_ldflags="$LDFLAGS"
LDFLAGS="$LDFLAGS -Wl,--version-script,conftest.map"
AC_LINK_IFELSE(
        [AC_LANG_PROGRAM(
                [[const char hello_world[] = "Hello, World\n";]],
                [[]])],
        [have_ld_with_version_script=yes],
        [have_ld_with_version_script=no]
)
LDFLAGS="$save_ldflags"

if test "x$have_ld_with_version_script" = "xyes"
then
        AC_MSG_RESULT([yes])
else
        AC_MSG_RESULT([no])
        AC_MSG_WARN([Linker does not support version scripts.])
fi

Here's what it produces when run with a linker that understands the --version-script flag:

mvescovi@kangaroo:~/version_script$ autoconf
mvescovi@kangaroo:~/version_script$ ./configure
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking whether linker supports version scripts... yes
mvescovi@kangaroo:~/version_script$

Oh yes, I release this code fragment under the GPL version 2 or later.