微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在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.

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

原文地址:https://www.jb51.cc/c/112363.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐