在GCC中使用16字节CAS时出现未定义的参考链接器错误 [英] Undefined reference linker error when using 16-byte CAS with GCC

查看:271
本文介绍了在GCC中使用16字节CAS时出现未定义的参考链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Debian上使用GCC 4.7.2,并且每当尝试使用具有16个字节值的< atomic> 工具时,都会出现链接器错误。我正在运行x86_64 VM,它可以支持 CMPXCHG16B 指令-但是即使我没有必要的硬件,我也看不到为什么链接器错误应该是在这里生产。据我所知,如果硬件不支持必要的CAS操作,则< atomic> 库应该转而使用常规锁。

I'm using GCC 4.7.2 on Debian, and getting linker errors whenever I try to use the <atomic> facilities with 16-byte values. I'm running an x86_64 VM which can support the CMPXCHG16B instruction - but even if I didn't have the necessary hardware, I don't see why a linker error should be produced here. As far as I know, the <atomic> library is supposed to fall back on using regular locks if the hardware doesn't support the necessary CAS operation.

无论如何,这是一个非常简单的测试用例来重现此问题:

Anyway, here's a very simple test case to reproduce this problem:

#include <atomic>
#include <cstdint>

struct foo
{
    std::uint64_t x;
    std::uint64_t y;
};

int main()
{
    std::atomic<foo> f1({0,0});
    foo f2 = {0,0};
    foo f3 = {1,1};
    f1.compare_exchange_strong(f2, f3);
}

编译时,我得到:

# g++ test.cpp -o test -std=c++11 -g3
/tmp/ccziKZis.o: In function `std::atomic<foo>::compare_exchange_strong(foo&, foo, std::memory_order, std::memory_order)':
/usr/include/c++/4.7/atomic:259: undefined reference to `__atomic_compare_exchange_16'
collect2: error: ld returned 1 exit status

请注意,如果我更改程序,则 foo 只有8个字节,我没有得到链接器错误。

Note that if I change the program so that foo is only 8 bytes, I don't get the linker error. What's going on here?

推荐答案

简单的答案,一旦您知道:

Simple answer, once you know it:

使用 -mcx16 调用 g ++

g ++文档说:


此选项将使GCC使用CMPXCHG16B
中的指令生成代码。 CMPXCHG16B允许对
128位双四字(或oword)数据类型进行原子操作。这对于可以由多个
处理器(或内核)更新的
高分辨率计数器很有用。该指令是
原子内置函数的一部分:有关详细信息,请参见* note Atomic Builtins ::。

This option will enable GCC to use CMPXCHG16B instruction in generated code. CMPXCHG16B allows for atomic operations on 128-bit double quadword (or oword) data types. This is useful for high resolution counters that could be updated by multiple processors (or cores). This instruction is generated as part of atomic built-in functions: see *note Atomic Builtins:: for details.

(请注意,这对于 clang 不起作用-我认为这是一个错误!)

(Note that this doesn't work for clang - I think that is a bug!)

这篇关于在GCC中使用16字节CAS时出现未定义的参考链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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