【问题标题】:ASM in C gives an error with -std=c99C 中的 ASM 使用 -std=c99 给出错误
【发布时间】:2009-04-16 10:07:35
【问题描述】:

我现在愿意使用-std=c99 编译我的项目,但我遇到了一个我暂时不理解的错误。这一行:

my_type* td = ({ register kmy_type* arg0 asm("eax"); arg0; });

仅在 C99 中给我以下错误:

warning: ISO C forbids nested functions
error: syntax error before ‘asm’
error: ‘arg0’ undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
warning: ISO C forbids braced-groups within expressions

欢迎任何线索帮助我理解这意味着什么。这行不是我写的,我也不确定它的目的是什么。

【问题讨论】:

  • 尝试改用 -std=gnu99。

标签: c assembly c99 inline-assembly


【解决方案1】:

线

my_type* td = ({ register my_type* arg0 asm("eax"); arg0; });

应该在eax 寄存器中获得一个值,解释为一个指针,指向td 变量。但是,它使用了许多 GNU 扩展,尤其是语句表达式和 asm(显式寄存器分配)的使用。我建议您切换到 -std=gnu99 (或其他名称)。否则,您可能想要使用双下划线(例如 asm -> __asm)或 __extension__ 关键字,但我不知道它是否有助于 c99 模式。

编辑:我刚刚尝试过,只需将 asm 更改为 __asm 即可。

【讨论】:

  • 谢谢 - 我也遇到了这个问题。 C99 不是很明显的问题!
【解决方案2】:

asm() 似乎不适用于-std=c99。在我看来这是个坏主意,因为标准实际上建议编译器支持 asm() 关键字,但这不是强制性的。

我建议你改用-pedantic

【讨论】:

  • 改用-std=gnu99,这样可以支持asm()函数。
  • @gbmhunter 与 11 标准相同(即 gnu11gnu++11)。感谢您的提示!
【解决方案3】:

问题不是asm,貌似是arg0

【讨论】:

  • 不,问题是asm; arg0 只是一个声明包含 asm 的变量,因此编译器对此感到困惑。
猜你喜欢
  • 2014-07-04
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 2021-07-29
  • 1970-01-01
相关资源
最近更新 更多