c语言main前什么都不加,C语言深度总结[全面认识main函数之前运行代码]

一、main运行前可运行哪些代码

(1)全局对象的构造函数会在main 函数之前执行。

(2)一些全局变量、对象和静态变量、对象的空间分配和赋初值就是在执行main函数之前,而main函数执行完后,还要去执行一些诸如释放空间、释放资源使用权等操作

(3)进程启动后,要执行一些初始化代码(如设置环境变量等),然后跳转到main执行。全局对象的构造也在main之前。

(4)通过关键字__attribute__,让一个函数在主函数之前运行,进行一些数据初始化、模块加载验证等。

main函数运行之前需要运行的逻辑(为了保持原味,我就不翻译了)

Some of the stuff that has to happen before main():

set up initial stack pointer

initialize static and global data

zero out uninitialized data

run global constructors

Some of this comes with the runtime library's crt0.o file or its __start() function. Some of it you need to do yourself.

Crt0 is a synonym for the C runtime library.

Depending on the system you're using the follwing may be incomplete, but it should give you an idea. Using newlib-1.9.0/libgloss/m68k/crt0.S as an outline, the steps are:1. Set stack pointer to value of __STACK if set

2. Set the initial value of the frame pointer

3. Clear .bss (where all the values that start at zero go)

4. Call indirect of hardware_init_hook if set to initialize hardware

5. Call indirect of software_init_hook if set to initialize software

6. Add __do_global_dtors and __FINI_SECTION__ to the atexit function so destructors and other cleanup functions are called when the program exits by either returning from main, or calling exit

7. setup the paramters for argc, argv, argp and call main

8. call exit if main returns

谁调用了我的main函数

我们都听说过一句话:“main是C语言的入口”。我至今不明白为什么这么说。就好像如果有人说:“挣钱是泡妞”,肯定无数砖头拍过来。这句话应该是“挣钱是泡妞的一个条件,只不过这个条件特别重要”。那么上面那句话应该是 “main是C语言中一个符号,只不过这个符号比较特别。”

我们看下面的例子:/* file name battle.c */

#include

int main(int argc, char* argv[])

{

printf("老铁,感谢有缘一起学习C语言,除了跟着这套课程走,为了更好地解决大家实操中的问题,可以加QQ群676593534,及时交流。群里还有很多精致资料哦,我这这里等你!");

return 0;

}

编译链接它:

cc battle.c -o test.exe

会生成 test.exe

但是我们加上这个选项: -nostdlib (不链接标准库)

cc battle.c-nostdlib -o test.exe

链接器会报错:

undefined symbol: __start

也就是说:

1. 编译器缺省是找 __start 符号,而不是 main

2. __start 这个符号是程序的起始点

3. main 是被标准库调用的一个符号

继续探索

我们写程序,比如一个模块,通常要有 initialize 和 de-initialize,但是我们写 C 程序的时候为什么有些模块没有这两个过程么呢?比如我们程序从 main 开始就可以 malloc,free,但是我们在 main 里面却没有初始化堆。再比如在 main 里面可以直接 printf,可是我们并没有打开标准输出文件啊。(不知道什么是 stdin,stdout,stderr 以及 printf 和 stdout 关系的群众请先看看 C 语言中文件的概念)。

有人说,这些东西不需要初始化。如果你真得这么想,有点率性而为了。

聪明的人民群众会想,一定是在 main 之前干了些什么。使这些函数可以直接调用而不用初始化。通常,我们会在编译器的环境中找到一个名字类似于 crt0.o 的文件,这个文件中包含了我们刚才所说的 __start 符号。(crt 大概是 C Runtime 的缩写)

那么真正的 crt0.s 是什么样子呢?下面我们给出部分伪代码:section .text:

__start:

:

init stack;

init heap;

open stdin;

open stdout;

open stderr;

:

push argv;

push argc;

call _main; (调用 main)

:

destory heap;

close stdin;

close stdout;

close stderr;

:

call __exit;

实际上可能还有很多初始化工作,因为都是和操作系统相关的,这里就不一一列出了。

注意:

1. 不同的编译器,不一定缺省得符号都是 __start。

2. 汇编里面的 _main 就是 C 语言里面的 main,是因为汇编器和C编译器对符号的命名有差异(通常是差一个下划线'_')。

Demo演示#include

#if 0

The constructor attribute causes the function to be called automatically before

execution enters main (). Similarly, the destructor attribute causes the function

to be called automatically after main () completes or exit () is called.

Functions with these attributes are useful for initializing data that is used

implicitly during the execution of the program.

more infoformation: https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes

#endif

__attribute__((constructor)) void before_main_to_run()

{

printf("Hi~,i am called before the main function!\n");

printf("%s\n",__FUNCTION__);

}

__attribute((constructor)) void before_main_to_run_two()

{

printf("Hi~,i am called before the main function!\n");

printf("%s\n",__FUNCTION__);

}

__attribute__((destructor)) void after_main_to_run()

{

printf("%s\n",__FUNCTION__);

printf("Hi~,i am called after the main function!\n");

}

__attribute((destructor)) void after_main_to_run_two()

{

printf("%s\n",__FUNCTION__);

printf("Hi~,i am called after the main function!\n");

}

int main( int argc, char ** argv )

{

printf("i am main function, and i can get my name(%s) by this way.\n",__FUNCTION__);

return 0;

}

总结

main函数执行之前,主要就是初始化系统相关资源:

1.设置栈指针

2.初始化static静态和global全局变量,即data段的内容

3.将未初始化部分的赋初值:数值型short,int,long等为0,bool为FALSE,指针为NULL,等等,即.bss段的内容

4.将main函数的参数,argc,argv等传递给main函数,然后才真正运行main函数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值