Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")))?

like image 542
bdonlan Avatar asked Oct 27 '25 09:10

bdonlan


1 Answers

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
like image 65
samplebias Avatar answered Oct 29 '25 00:10

samplebias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!