GNU C global register variables不能有初始化程序。这不会编译为 C 或 C++:
// at global scope.
register int i asm ("r12") = 10;
给出 ( Godbolt ) 错误:全局寄存器变量具有初始值
。本地作用域当然没问题,但 GNU C 本地寄存器变量是完全不同的事情。 (仅保证在与扩展 asm()
语句交互方面执行任何操作。)
代码
#include<stdio.h>
register int i asm ("r12"); //how to initialize i here?
int main()
{
i=10; // Would rather avoid this workaround
printf("%d\n",i);
}
如何在全局范围内初始化i
,而不是等到main的顶部?
最佳答案
您无法初始化全局寄存器变量。
GCC documentation声明如下:
Global register variables cannot have initial values, because an executable file has no means to supply initial contents for a register.
另请注意下面的段落:
When selecting a register, choose one that is normally saved and restored by function calls on your machine. This ensures that code which is unaware of this reservation (such as library routines) will restore it before returning.
您不应该使用r12
,它不会在调用之间保存。
关于c - 如何初始化 GNU C 全局寄存器变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58321181/