只是C ++ 11的一些代码:
#include<iostream>
#include<atomic>
struct A { int a[4]; };
struct B { int x, y; };
int main()
{
std::cout << std::boolalpha
<< "std::atomic<A> is lock free? "
<< std::atomic<A>{}.is_lock_free() << '\n'
<< "std::atomic<B> is lock free? "
<< std::atomic<B>{}.is_lock_free() << '\n';
}
使用mac + clang编译,它会出错:
Undefined symbols for architecture x86_64:
"___atomic_is_lock_free", referenced from:
_main in atomics.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
clang + ubuntu docker给出:
root@b01946bedcf2:/# clang++ --version
clang version 4.0.0-1ubuntu1 (tags/RELEASE_400/rc1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
root@b01946bedcf2:/# clang++ 1.cpp -std=c++11 -lpthread
/tmp/1-7cc6e9.o: In function `std::atomic<A>::is_lock_free() const':
1.cpp:(.text._ZNKSt6atomicI1AE12is_lock_freeEv[_ZNKSt6atomicI1AE12is_lock_freeEv]+0x1b): undefined reference to `__atomic_is_lock_free'
/tmp/1-7cc6e9.o: In function `std::atomic<B>::is_lock_free() const':
1.cpp:(.text._ZNKSt6atomicI1BE12is_lock_freeEv[_ZNKSt6atomicI1BE12is_lock_freeEv]+0x1b): undefined reference to `__atomic_is_lock_free'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在RHEL7 + gcc4.8.5上,它给出了:
$g++ 1.cpp -std=c++11 -lpthread && ./a.out
/tmp/ccW9pNc2.o: In function `std::atomic<A>::is_lock_free() const':
1.cpp:(.text._ZNKSt6atomicI1AE12is_lock_freeEv[_ZNKSt6atomicI1AE12is_lock_freeEv]+0x17): undefined reference to `__atomic_is_lock_free'
collect2: error: ld returned 1 exit status
这真的很奇怪。代码有什么问题,还是需要超高版本的编译器?