The following C++ 11 program fails to link with GCC (both 4.9 and 5.0) because the primary std::atomic template allocates 3 bytes for both a and aa and calls __atomic_store(&aa.a, &tmp, __ATOMIC_SEQ_CST). GCC only supports calls to __atomic_store with power of 2 arguments of at most 8. Assuming the code is well-defined (I couldn't find anything to suggest otherwise -- the type meets the trivially-copyable requirement) it seems that specializations of the template on types of such sizes should add padding up to the nearest greater power of 2 and increase their own alignment so as to make it possible to manipulate them atomically. Larger specializations will need to make use of a mutex. $ cat a.cpp && g++ -Wall -Wpedantic -std=c++11 a.cpp #include <atomic> int main (void) { struct A { char a [3]; } a = { { 'a', 'b', 'c' } }; std::atomic<A> aa; aa.store (a); } /tmp/ccGQyZte.o: In function `std::atomic<main::A>::store(main::A, std::memory_order)': a.cpp:(.text+0xf0): undefined reference to `__atomic_store' collect2: error: ld returned 1 exit status
You need to link to libatomic for objects with sizes that aren't supported natively by the CPU.
I see. Thanks. With that fixed, the program links successfully. It would be nice if GCC gave a better message for this sort of a user error. I recall a compiler (HP aCC maybe, or MSVC) that suggested a library to link with when the linker failed with an unsatisfied reference.
Interesting. For some programs and libraries, GCC too gives a diagnostic that's more helpful than just: undefined reference to `foo' /usr/bin/ld: /tmp/cccCqEak.o: undefined reference to symbol 'pthread_create@@GLIBC_2.2.5' /lib64/libpthread.so.0: error adding symbols: DSO missing from command line
That is only for libraries visible to the linker indirectly (dependencies of other libraries).