【问题标题】:Compiler complaining about volatile keyword编译器抱怨 volatile 关键字
【发布时间】:2018-11-16 21:13:57
【问题描述】:

我试图从this question 编译以下代码,但编译失败:error C2059: syntax error: 'volatile'

#include<stdint.h>
#include<stdio.h>

static inline uint64_t rdtscp( uint32_t & aux )
{
    uint64_t rax,rdx;
    asm volatile ( "rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (aux) : : );
    return (rdx << 32) + rax;
}

我使用 x64 msvc v19(WINE) 编译器,godbolt 上没有任何标志

【问题讨论】:

  • 您希望volatile 在那里做什么?
  • 这似乎是 GCC 特有的语法。
  • 这不是我的代码,正在尝试编译代码。另外我认为“volatile”用于避免编译器进行任何调度
  • 是 C 还是 C++?
  • @NeilButterworth volatile 将阻止删除可访问的 asm 代码,即使其结果似乎没有在程序的其余部分中使用;它还可以防止它被语言级别的指令重新排序。

标签: c++ c compiler-errors


【解决方案1】:

asm volatile 是一个 GNU 扩展。限定符是documented here

对于 MSVC,请改用 __rdtscp intrinsic


另外,请注意,您可以在所有主要编译器中使用内在函数,例如:

#include <iostream>
#include <cstdint>

#ifdef _WIN32
#  include <intrin.h>
#else
#  include <x86intrin.h>
#endif

int main()
{
    uint64_t i;
    uint32_t ui;
    i = __rdtscp(&ui);
    std::cout
        << "Ticks: " << i << '\n'
        << "TSC_AUX: " << ui << '\n'
    ;
    return 0;
}

【讨论】:

    猜你喜欢
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2011-09-06
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多