8

The following code fails to link:

#include <atomic>                                                                                                                                                             
struct A
{
    unsigned long a;
    unsigned long b;
};                                                                                                                                                           
struct B
{
    void set(A tmp)
    {
        _a.store(tmp);
    }
    std::atomic<A> _a;
 };                                                                                                                                                           

 int main()
{
   B b;
   b.set(A());
   return  0;
}

With the following error:

/tmp/cc8gyaZM.o: In function `std::atomic<A>::store(A, std::memory_order)':
dryn.cpp:     (.text._ZNSt6atomicI1AE5storeES0_St12memory_order[_ZNSt6atomicI1AE5storeES0_St12memory_order]+0x3e): undefined reference to `__atomic_store_16'

If I replace the unsigned long-s with anything that is up-to int in size, this compiles just fine. Using g++ 4.7.2 . Do you have any idea why is that?

Compiled with command:

g++ dryn.cpp --std=c++11

1 Answers1

7

As answered by Zeta:

Atomic API isn't complete in GCC 4.7:

  • When lock free instructions are not available (either through hardware or OS support) atomic operations are left as function calls to be resolved by a library. Due to time constraints and an API which is not finalized, there is no libatomic supplied with GCC 4.7. This is easily determined by encountering unsatisfied external symbols beginning with __atomic_*.

Since there is no libatomic shipped with GCC 4.7 you need to use another compiler which actually supports the features you want or provide the missing features (sample implementation).

Community
  • 1
  • 1
Alex
  • 467
  • 4
  • 13
  • comments: Install libatomic to fix it. (note that in GCC the dll named 'libatomic.so.1' , so using -l:libatomic.so.1 to linking) – vrqq Mar 14 '23 at 15:20