本文介绍了有什么用_start()在C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的同事了解到,一个能写和不写的main()函数执行的 C 的程序。这是可以做到的在

I learned from my colleague that one can write and execute a C program without writing a main() function. It can be done as under

withoutMain.c

// Compile it with gcc -nostartfiles

void _start() {
  int ret = my_main();
  exit(ret);
}

int my_main() {
  puts("This is a program without main!\n");
  return 0;
}

编译如下:
的gcc -o withoutMain withoutMain.c -nostartfiles

运行它:
./ withoutMain

我的问题是什么时候总会有需要做这种事情?一些真实的场景?

My question is When would one need to do this kind of thing ? Some real world scenario?

推荐答案

符号 _start 是程序的入口点的。也就是说,该符号的地址都是跃升至在程序启动。通常情况下,该名称的功能 _start 是由一种叫做的crt0.o 文件,其中包含启动$ C $提供c表示C运行时环境。它设置了一些东西,填充参数数组的argv ,计数很多争论如何在那里,然后调用。后的回报,退出被调用。

The symbol _start is the entry point of your program. That is, the address of that symbol is the address jumped to on program start. Normally, the function with the name _start is supplied by a file called crt0.o which contains the startup code for the C runtime environment. It sets up some stuff, populates the argument array argv, counts how many arguments are there, and then calls main. After main returns, exit is called.

如果程序不希望使用C运行时环境,它需要提供自己的code为 _start 。例如,围棋编程语言的参考实现这样做,因为他们需要这需要一些魔术与堆栈非标准线程模型。这也是非常有用的提供自己的 _start 当你想写真正的小程序或做非常规的事情节目。

If a program does not want to use the C runtime environment, it needs to supply its own code for _start. For instance, the reference implementation of the Go programming language does so because they need a non-standard threading model which requires some magic with the stack. It's also useful to supply your own _start when you want to write really tiny programs or programs that do unconventional things.

这篇关于有什么用_start()在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:40