[C/C++] The program executes code before/after main|prints the compilation date before main

table of Contents

The program executes code before/after main

Print compilation date


The program executes code before/after main

http://www.mamicode.com/info-detail-2087871.html

  Before the main function, there will be a series of initialization operations, such operations are usually completed by the linker and so on.

Specifically, the earliest function executed by the program is not main. For example, in windows, this function is mainCRTStartup. This function is executed by the linker to initialize the runtime library. This function will call the CRTInit function, which will affect C Global variables, C memory allocation, and global class objects and constructors in C++ are initialized. So it is possible to execute some of your own code before the main function.

1. Utilize the __attribute keyword of gcc in Linux

       In C programming in the Linux environment, you can use the __attribute keyword to define constructor and destructor . The former will be executed before the main function , and the latter will be executed after the main function .

       code show as below:

 #include <stdio.h>
 
__attribute((constructor)) void before_main()
 {
     printf("before main!\n");
 }
 
  __attribute((destructor)) void after_main()
  {
     printf("after main!\n");
 }
 
 int main(void)
 {
     printf("This is main function.\n");
     return 0;
 }

before_main.c

       operation result:

natalie@ubuntu:~/Desktop/zhou_it_c/before_main$ gcc before_main.c -o before_main

natalie@ubuntu:~/Desktop/zhou_it_c/before_main$ ./before_main
before main!
This is main function.
after main!

2. Use #pragma pre-defined in Windows environment

       We said above that some initialization work will be done in the CRTInit function, including C library, C initialization function, C++ library, C++ initialization function, etc. C and C++ each have a table to store the initialization function pointers, and each table uses 2 pointers to clarify the scope. In the initialization process, the __CRTInit function will call the functions in these two tables at once, so if we can put the function to be executed in these two tables, then 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]

       When we specifically execute, we put the function to be executed in the section by defining special section names ".CRT$XIU" and ".CRT$XCU". The linker will form a table of C initialization functions:

       [__xi_a, ..., before1(xiu), ..., __xi_z]

       And C++ initialization function table:

       [__xc_a, ..., before2(xcu), ..., __xc_z]

       code show as below:

#include <stdio.h>

int before_main(void)
{
    printf("before main!\n");
    return 0;
}

typedef int func();

#pragma data_seg(".CRT$XIU")
static func *before[] = { before_main };
#pragma data_seg()


int main(void)
{
    printf("This is main function.\n");
    return 0;
}

 

before_main.c

3. Use to define global class objects or global variables in C++ programming

       mainCRTStartup initializes the global object a, which means that the constructor of a will be executed before main , so we only need to define the function we want to execute in the constructor of a .

       Another way is to define a global variable as the structure after the function runs, then the function will be used for initialization and will be executed before main.

       code show as below:

 #include <iostream>
 using namespace std;
 using std::cout;
 
 int func()
  {
      cout <<"before main: func()" << endl;
      return 0;
  }
 
 class A
 {
 public:
     A()
     {
         cout << "A() constructor" << endl;
     }
     ~A()
     {
         cout << "A() destructor" << endl;
     }
 };
 
 A a;
 
 int g_iValue = func();
 
 int main(void)
 {
     cout << "This is main function." << endl;
     return 0;
 }

before_main.cpp

       operation result:

A() constructor
before main: func()
This is main function.
A() destructor

 

Print compilation date

https://blog.csdn.net/bandaoyu/article/details/114704021

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/114760738