加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么g仍然需要-latomic

发布时间:2020-12-16 03:10:37 所属栏目:百科 来源:网络整理
导读:在29.5原子类型的C标准2014年11月的工作草案中指出: There is a generic class template atomic. The type of the template argument T shall be trivially copyable (3.9). [ Note: Type arguments that are not also statically initializable may be dif
在29.5原子类型的C标准2014年11月的工作草案中指出:
  1. There is a generic class template atomic. The type of the template argument T shall be trivially copyable (3.9). [ Note: Type arguments that are not also statically initializable may be difficult to use. —end note ]

所以 – 据我所知 – 这个:

#include <atomic>

struct Message {
    unsigned long int a;
    unsigned long int b;
};

std::atomic<Message> sharedState;

int main() {    
    Message tmp{1,2};       
    sharedState.store(tmp);         
    Message tmp2=sharedState.load();
}

应该是完全有效的标准c 14(也是c 11)代码.但是,如果我不手动链接libatomic的命令

g++ -std=c++14 <filename>

给出 – 至少在Fedora 22(gcc 5.1)) – 以下链接错误:

/tmp/ccdiWWQi.o: In function `std::atomic<Message>::store(Message,std::memory_order)':
main.cpp:(.text._ZNSt6atomicI7MessageE5storeES0_St12memory_order[_ZNSt6atomicI7MessageE5storeES0_St12memory_order]+0x3f): undefined reference to `__atomic_store_16'
/tmp/ccdiWWQi.o: In function `std::atomic<Message>::load(std::memory_order) const':
main.cpp:(.text._ZNKSt6atomicI7MessageE4loadESt12memory_order[_ZNKSt6atomicI7MessageE4loadESt12memory_order]+0x1c): undefined reference to `__atomic_load_16'
collect2: error: ld returned 1 exit status

如果我写

g++ -std=c++14 -latomic <filename>

一切安好.
我知道标准没有说明任何关于编译器标志或必须包含的库,但到目前为止,我认为可以通过第一个命令编译任何标准的单一文件代码.

那么为什么这不适用于我的示例代码?是否有理由为什么-latomic仍然是必需的,还是只是编译器维护者还没有解决的问题呢?

解决方法

Relevant reading在GCC主页上关于GCC如何以及为什么在某些情况下使< atomic>首先.

GCC和libstdc只是失去耦合. libatomic是库的领域,而不是编译器,您可以使用GCC与不同的库(可能为其主要正确或不同名称提供必要的< atomic>定义),所以GCC不能只是假设-latomic.

Also:

GCC 4.7 does not include a library implementation as the API has not been firmly established.

同一页面声称GCC 4.8将提供这种图书馆的实施,但计划是战争的第一个受害者.我猜想在这个附近可以找到原子仍然是必要的原因.

除了…

…so far I thought that any standard conformant,single file code can be compiled via the first command.

如果您正在使用数学函数,…已经有一段时间了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读