c++ - g++ 内联调用 always_inline "int _rdrand16_step()"失败

标签 c++ g++

我使用英特尔函数 _rdrand16_step() 编写了一段代码,在 Windows(Visual Studio 2017)上运行良好,但在 Linux(g++)上我无法使其运行。 我在我的代码中调用了该函数 2 次:

#include <immintrin.h>
...
unsigned short val = 0;
if (_rdrand16_step(&val))
...
_rdrand16_step(&val);
...

g++ 输出如下:

/usr/lib/gcc/x86_64-linux-gnu/8/include/immintrin.h: In member function ‘int otp_s7c::crypt(std::__cxx11::string, std::__cxx11::string, long long unsigned int)’:
/usr/lib/gcc/x86_64-linux-gnu/8/include/immintrin.h:129:1: error: inlining failed in call to always_inline ‘int _rdrand16_step(short unsigned int*)’: target specific option mismatch
 _rdrand16_step (unsigned short *__P)
 ^~~~~~~~~~~~~~
otp_s7c.cpp:139:24: note: called from here
      if (_rdrand16_step(&val))
          ~~~~~~~~~~~~~~^~~~~~
/usr/lib/gcc/x86_64-linux-gnu/8/include/immintrin.h:129:1: error: inlining failed in call to always_inline ‘int _rdrand16_step(short unsigned int*)’: target specific option mismatch
 _rdrand16_step (unsigned short *__P)
 ^~~~~~~~~~~~~~
otp_s7c.cpp:148:23: note: called from here
         _rdrand16_step(&val);
         ~~~~~~~~~~~~~~^~~~~~

最佳答案

这是一个有点误导性的错误消息,因为您实际上并没有告诉编译器您的目标体系结构支持 RDRAND 指令(据我所知,这里的重要部分是“目标特定选项”不匹配”部分在最后)。

-mrdrnd 添加到编译器标志似乎可以解决问题。


比较 Compiler Explorer 上的示例 withwithout旗帜

关于c++ - g++ 内联调用 always_inline "int _rdrand16_step()"失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52224421/

相关文章:

c++ - 编译错误: “lvalue required as left operand of assignment” Unsure why

c++ - 从偏移量读取写入

c++ - __uint128_t 不适用于 Clang 和 libstdc++

c++ - 不,真的,浮点提升什么时候发生?

c++ - 在constexpr上下文中验证std::initializer_list

c++ - 交叉编译c++到iphone arm

c++ - 当一个参数是函数的返回值时调用 pow() 时出现链接器错误

linux - g++ 默认 header 包含列表

c++ - 静态链接 C++,找不到库

c++ - 符号 (&) 在 .rc 文件中的含义