Search code examples
cshared-librariesportabilityautoconfsymbols

Best practices for probing for symbol visibility extensions in autoconf


I'd like to add symbol hiding for internal symbols to an existing library with an autoconf-based build system. What's the best way to probe for the local compiler's equivalent for -fvisibility=hidden and __attribute__ ((visibility("default")))?


Solution

  • I don't think there is a standard macro for this, but here is something which you should be able to extend to support other compilers as needed, e.g. __hidden for Sun compilers:

    AC_CACHE_CHECK([for __attribute__((visibility("hidden")))],
        ac_cv_hidden_visibility_attribute, [
        echo 'int __attribute__ ((visibility ("hidden"))) foo (void) { return 1; }' > conftest.c
        ac_cv_hidden_visibility_attribute=no
        if AC_TRY_COMMAND(${CC-cc} -Werror -S conftest.c -o conftest.s 1>&AS_MESSAGE_LOG_FD);
        then
            if grep '\.hidden.*foo' conftest.s >/dev/null;
            then
                ac_cv_hidden_visibility_attribute=yes
            fi
        fi
        rm -f conftest.*
        ])
    if test $ac_cv_hidden_visibility_attribute = yes;
    then
        AC_DEFINE(HAVE_HIDDEN_VISIBILITY_ATTRIBUTE, 1,
              [Define if __attribute__((visibility("hidden"))) is supported.])
    fi
    

    The resulting config.h:

    /* Define if __attribute__((visibility("hidden"))) is supported. */
    #define HAVE_HIDDEN_VISIBILITY_ATTRIBUTE 1