为什么std :: atom :: is_lock_free无法编译? [英] Why std::atom::is_lock_free doesn't compile?

查看:89
本文介绍了为什么std :: atom :: is_lock_free无法编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需一段C ++ 11的代码:

Just a bit code of 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编译,会出现错误:

Compile with mac+clang, it gives error:

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提供:

clang + ubuntu docker gives:

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上,它给出:

And on RHEL7+gcc4.8.5, it gives:

$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

这真的很奇怪.代码有什么问题,还是需要超高版本的编译器?

This is really odd. What's wrong with the code, or does it require a super-high version of compiler?

推荐答案

您需要包含 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆