程序执行必须从main开始,还是可以修改起始地址?
#include <stdio.h>
void fun();
#pragma startup fun
int main()
{
printf("in main");
return 0;
}
void fun()
{
printf("in fun");
}
该程序打印
in fun
之前 in main
.
最佳答案
The '#pragma' command is specified in the ANSI standard to have an arbitrary implementation-defined effect. In the GNU C preprocessor, '#pragma' first attempts to run the game 'rogue'; if that fails, it tries to run the game 'hack'; if that fails, it tries to run GNU Emacs displaying the Tower of Hanoi; if that fails, it reports a fatal error. In any case, preprocessing does not continue.
-- Richard M. Stallman,GNU C 预处理器,版本 1.34
程序执行从启动代码或“运行时”开始。这通常是一些名为
_start
的汇编程序。或类似的,位于(在 Unix 机器上)文件中 crt0.o
随编译器包一起提供。它执行运行 C 可执行文件所需的设置(例如,设置 stdin
、 stdout
和 stderr
,atexit()
使用的 vector ... 对于 C++ 它还包括全局对象的初始化,即运行它们的构造函数)。只有这样控制才会跳转到main()
。 .正如我的答案开头的引述如此 Eloquent 地表达的那样,
#pragma
是否完全取决于您的编译器。检查其文档。 (我猜你的 pragma startup
- 应该在前面加上 #
- 告诉运行时首先调用 fun()
...)
关于c - 程序执行是否总是从 C 中的 main 开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6897763/