之前的主执行code() [英] Executing code before main()
问题描述
在面向对象语言(C ++),你可以在执行code的main()
通过一个全局对象或类的静态对象,并有自己的构造函数运行在code你想要的。
In object-oriented languages (C++) you can execute code before main()
by using a global object or a class static object and have their constructors run the code you want.
有没有办法在C这样做吗?我没有我试图解决任何具体问题,我只是好奇。一方面,这可能是有用的是自动初始化的库。
Is there any way to do this in C? I don't have any specific problem I'm trying to solve, I'm just curious. One thing this might be useful for is automatically initializing a library.
推荐答案
有使用方法 __属性__
但这些都是非常具体的编译器和code,它是使用这些都是写的不可以真正便携。 OTOH,在 C
语言不提供任何启动模块/库。
There are ways using __attribute__
but those are very specific to your compiler and code that is written using these are not really portable. OTOH, the C
language does not provide any start-up modules/library.
在 C
,逻辑的main()
是由操作系统调用的第一个函数。但调用之前的main()
,OS调用称为另一个函数启动
模块设置各种环境变量,初始化(未初始化)静态变量,建立一个堆栈帧(活动记录)和堆栈指针初始化堆栈区的起始和具有调用main()$之前来完成其他任务C $ C>。
In C
, logically main()
is the first function called by the OS. But before calling main()
, the OS calls another function called start-up
module to setup various environment variables, initialize (un-initialized) static variables, build a stack frame (activation record) and initialize the stack pointer to the start of the stack area and other tasks that have to be done before calling main()
.
说,如果你正在写code对于嵌入式系统里没有,或者 - 最低限度的操作系统做上述工作,那么你应该探讨这些选项这是编译器的依赖。除了 GCC
, Turbo和
Microsoft` C编译器提供了设施,以增加code在特定的硬件设备( FE 8086机)。
Say if you are writing code for embedded systems where there is no-or-minimal OS to do the above mentioned work, then you should explore these options which are compiler dependent. Other than GCC
, Turbo and
Microsoft` C compilers provides facilities to add code in a particular hardware machine (f.e. 8086 machines).
IOW,在启动模块并不意味着程序员。
IOW, the start-up modules are not meant for the programmers.
这篇关于之前的主执行code()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!