在C / C ++程序如何系统(Windows,Linux和Mac OS X的)调用main()函数 [英] In a C/C++ program how does the system (windows, linux, mac OS X) call the main() function

查看:102
本文介绍了在C / C ++程序如何系统(Windows,Linux和Mac OS X的)调用main()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个更技术性的解释则操作系统调用该函数。
任何人都可以帮我或点我到一个网站或预订?

I am looking for a more technical explanation then the OS calls the function. Can anyone help me out or point me to a website or book?

感谢。

推荐答案

.exe文件(或等值其他平台)包含一个切入点地址。在第一近似,OS装载.EXE的相关部分文件到RAM中,然后跳转到入口点。

The .exe file (or equivalent on other platforms) contains an 'entry point' address. To a first approximation, the OS loads the relevant sections of the .EXE file into ram, and then jumps to the entry point.

正如其他人所说,这个入口点不会是'主',但反而会是运行时库的一部分 - 它会做这样的事情初始化静态对象,建立ARGC / argv的参数,设置标准输入/标准输出/标准错误等。当它完成了这一切,它会打电话给你的main()函数。当主退出,运行时经历过你的回报code回到环境中,调用静态析构函数,调用_atexit程序等,以相似的过程。

As others have said, this entry point will not be 'main', but will instead be a part of the runtime library - it will do things like initialising static objects, setting up the argc/argv parameters, setting up stdin/stdout/stderr, etc. When it's done all that, it will call your main() function. When main exits, the runtime goes through an analagous process of passing your return code back to the environment, calling static destructors, calling _atexit routines, etc.

如果您有MS工具(也许不是免费搭乘的),然后你把所有的运行来源,一种简单的方式来看待它是把一个断点在你的main()方法的右括号,以及单退一步成的运行时间。

If you have MS tools (perhaps not the freebie ones), then you have all the runtime source, and an easy way to look at it is to put a breakpoint on the closing brace of your main() method, and single step back up into the runtime.

这篇关于在C / C ++程序如何系统(Windows,Linux和Mac OS X的)调用main()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆