问题
#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
回答1:
You need to compile the program with the -latomic
flag on clang and gcc. demo.
来源:https://stackoverflow.com/questions/44891523/undefined-reference-atomic-is-lock-free-with-xcode-8-2-1