Problem with C11 _Atomic
Konstantin Belousov
kostikbel at gmail.com
Mon Jan 1 21:09:21 UTC 2018
On Mon, Jan 01, 2018 at 09:47:40PM +0100, Pierre DAVID wrote:
> Hi,
>
> I'm on a recent current:
> FreeBSD biceps.ma.maison 12.0-CURRENT FreeBSD 12.0-CURRENT #2 r327239: Wed Dec 27 18:25:46 CET 2017 pda at biceps.ma.maison:/usr/obj/usr/src/amd64.amd64/sys/BICEPS amd64
>
> with clang 5.0.1:
> FreeBSD clang version 5.0.1 (tags/RELEASE_501/final 320880) (based on LLVM 5.0.1)
> Target: x86_64-unknown-freebsd12.0
> Thread model: posix
> InstalledDir: /usr/bin
>
> I'm having a problem with the following source file:
>
> ------------------------------------------------------------------------------
> #include <stdatomic.h>
>
> struct foo
> {
> int f1 ;
> char f2 ;
> int f3 ;
> } ;
>
> _Atomic struct foo a ;
> struct foo b ;
>
> int main (int argc, char *argv [])
> {
> b = (struct foo) {.f1 = 5, .f2 = 7, .f3 = 9 } ;
> // atomic_store (&a, b) ;
> a = b ;
> }
> ------------------------------------------------------------------------------
>
> This code does not compile/link with:
> % cc foo.c -lstdthreads
> /tmp/foo-a0ef26.o: In function `main':
> foo.c:(.text+0x63): undefined reference to `__sync_lock_test_and_set_16'
> cc: error: linker command failed with exit code 1 (use -v to see invocation)
>
> The gcc internal seems to be linked as "cc -v" told me:
> % cc -v foo.c -lstdthreads
> ...
> "/usr/bin/ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 --hash-style=both --enable-new-dtags -o a.out /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtbegin.o -L/usr/lib /tmp/foo-d7a21b.o -lstdthreads -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/crtend.o /usr/lib/crtn.o
>
> The problem occurs with "atomic_store(&a, b)" as well as with "a = b".
>
> If I remove the f3 member, the struct foo is only 8 bytes and the code
> compiles/links.
>
> Did I missed something?
clang issues a calls to libatomic, which we do not provide.
As a workaround, use the following command to compile. The resulting
binary works on all practically usable machines.
$ cc -march=core2 source.c
You might want to turn off sse3/4.1 if you are concerned about older pentium4.
More information about the freebsd-current
mailing list