vy32
vy32

Reputation: 29655

How do I declare __sync_fetch_and_add?

I am writing a program that could very much use __sync_fetch_and_add. Unfortunately my autoconf script that searches for it with this fairly straightforward test:

AC_CHECK_FUNCS([__sync_fetch_and_add])

Generates this error:

Wsuggest-attribute=noreturnconftest.c:56:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
conftest.c:56:6: warning: conflicting types for built-in function '__sync_fetch_and_add' [enabled by default]
conftest.c:65:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
/tmp/ccqPsZz4.o: In function `main':
/home/simsong/domex/src/bulk_extractor/tags/1.2.x/conftest.c:67: undefined reference to `__sync_fetch_and_add'
collect2: ld returned 1 exit status

This is super-annoying, because I would like to use the function, but some people on some platforms have told me that it doesn't compile properly. I want a prototype, but there doesn't seem to be one.

Thanks.

Upvotes: 4

Views: 2712

Answers (1)

jørgensen
jørgensen

Reputation: 10549

AC_CHECK_FUNCS is not usable in this case, since redeclarating the function — which autoconf does (char functionthatiwant() in conftest.c/config.log) — will override the builtin function detrimentally. You would need something like the following instead.

AC_MSG_CHECKING([for __sync_fetch_and_add])
AC_LINK_IFELSE(
   [AC_LANG_SOURCE([
    int main(void) { return __sync_fetch_and_add((int *)0, 0); }
   ])],
   [AC_MSG_RESULT([yes])],
   [AC_MSG_RESULT([no])]
)

Upvotes: 4

Related Questions