Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference ___atomic_is_lock_free with Xcode 8.2.1 [duplicate]

#include <atomic>
#include <iostream>

using namespace std;

struct Simple{
    int a = 0;
    int b = 0;
};

struct WithPointer{
    int *a = nullptr;
    int b = 0;
};

int main(int argc, char const *argv[])
{
    atomic<Simple> simple;
    cout<<simple.is_lock_free()<<"\n";
    
    atomic<Simple*> simple_p;
    cout<<simple_p.is_lock_free()<<"\n";

    atomic<WithPointer> with_pointer;
    cout<<with_pointer.is_lock_free()<<"\n";

    return 0;
}

This example works fine for the Simple struct but not for the WithPointer struct. I get the following compile error, why? What can I do.

g++ main.cpp
/usr/bin/ld: /tmp/cc49YEoR.o: in function `std::atomic<WithPointer>::is_lock_free() const':
1a.cpp:(.text._ZNKSt6atomicI11WithPointerE12is_lock_freeEv[_ZNKSt6atomicI11WithPointerE12is_lock_freeEv]+0x1d): undefined reference to `__atomic_is_lock_free'
collect2: error: ld returned 1 exit status
like image 999
birneee Avatar asked Nov 19 '22 16:11

birneee


1 Answers

You need to compile the program with the -latomic flag on clang and gcc. demo.

like image 141
cigien Avatar answered Dec 05 '22 08:12

cigien