Source: "Zhou Go it.c Language Deep learning utilization" https://ke.qq.com/course/242707#tuin=a71606
When we learn the C + + language, we usually think that the main function is the beginning of the entire program execution. In fact, before the main function, there will be a series of initialization operations, usually done by the linker, and so on. Specifically, the first function of the program is not actually main, in Windows, is mainCRTStartup, this function is the linker to initialize the runtime library, this function will call the Crtinit function, the function of C global variables, C memory allocations and C + + Initializes the global class objects and constructors in the So it is possible to execute some of your own code before the main function.
1. __attribute keyword using GCC in linux environment
In C programming for Linux environments, you can use the __attribute keyword to define constructor and destructor, where the former executes before the main function, which executes after the main function.
The code is as follows:
1#include <stdio.h>2 3__attribute ((constructor))voidBefore_main ()4 {5printf"before main!\n");6 }7 8__attribute ((destructor))voidAfter_main ()9 {Tenprintf"After main!\n"); One } A - intMainvoid) - { theprintf"This is main function.\n"); - return 0; -}before_main.c
Operation Result:
[Email protected]:~/desktop/zhou_it_c/before_main$ gcc before_main.c-o before_main
[Email protected]:~/desktop/zhou_it_c/before_main$./before_main
Before main!
This is main function.
After main!
2. Using #pragma pre-defined in the Windows environment
As we said above, we will do some initialization work in the Crtinit function, including C library, c initialization function, C + + library, C + + initialization function, etc. C and C + + each have a table to hold the initialization function pointers, and each table uses 2 pointers to clarify the scope. During initialization, the __CRTINIT function invokes the functions in both tables at once, so if we can put the functions that we want to execute in both tables, we can achieve the purpose of executing the code before main.
The scope of the C initialization function table is: [__xi_a, __xi_a] The scope of the C + + initialization function table is: [__xc_a, __xc_z]
We do this by defining a special segment name ". Crt$xiu "and". Crt$xcu ", place the function to be executed in the segment. The linker forms the Kusakabe C initialization function table:
[__xi_a, ..., Before1 (Xiu), ..., __xi_z]
and the C + + initialization function table:
[__xc_a, ..., Before2 (XCU), ..., __xc_z]
The code is as follows:
#include <stdio.h>intBefore_main (void) {printf ("before main!\n"); return 0;} typedefintfunc ();#pragmaData_seg (". Crt$xiu ")StaticFunc *before[] ={before_main};#pragmaData_seg ()intMainvoid) {printf ("This is main function.\n"); return 0;}before_main.c
3. Using the Define global class object or global variable in C + + programming
mainCRTStartup initializes the global object A, which means that the construction of a has a tax that precedes main, so you only need to define the function we want to execute in the constructor of a.
Another way is to define a global variable to be the structure after the function is run, then the function is used for initialization, which is executed before main.
The code is as follows:
1#include <iostream>2 using namespacestd;3 usingstd::cout;4 5 intfunc ()6 {7cout <<"before Main:func ()"<<Endl;8 return 0;9 }Ten One classA A { - Public: - A () the { -cout <<"A () constructor"<<Endl; - } -~A () + { -cout <<"A () destructor"<<Endl; + } A }; at - A; - - intG_ivalue =func (); - - intMainvoid) in { -cout <<"This is main function."<<Endl; to return 0; +}Before_main.cpp
Operation Result:
A () constructor
Before Main:func ()
This is main function.
A () destructor
C + + programs execute code before main