如何初始化 GNU C 全局寄存器变量
How to initialize a GNU C global register variable
GNU C global register variables 不能有初始值设定项。这不会编译为 C 或 C++:
// at global scope.
register int i asm ("r12") = 10;
给出 (Godbolt) error: global register variable has initial value
。局部作用域当然很好,但是 GNU C local 寄存器变量是一个非常不同的东西。 (只保证在与扩展 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 的顶部?
您不能初始化全局寄存器变量。
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
,它不会在调用时保存。
GNU C global register variables 不能有初始值设定项。这不会编译为 C 或 C++:
// at global scope.
register int i asm ("r12") = 10;
给出 (Godbolt) error: global register variable has initial value
。局部作用域当然很好,但是 GNU C local 寄存器变量是一个非常不同的东西。 (只保证在与扩展 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 的顶部?
您不能初始化全局寄存器变量。
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
,它不会在调用时保存。