如何在main()函数之前执行一些代码

版权声明:欢迎转载,注明出处 https://blog.csdn.net/youyou519/article/details/82703448

在控制台程序中,main函数是用户定义的执行入口点,当程序编译成功之后,链接器(Linker)会将mainCRTStartup连接到exe中,exe执行时,一开始先mainCRTStartup,这是因为程序在执行时会调用各种各样的运行时库函数,因此执行前必须要初始化好运行时库,mainCRTStartup函数会负责相应的初始化工作,他会完成一些C全局变量以及C内存分配等函数的初始化工作,如果使用C++编程,还要执行全局类对象的构造函数。最后,mainCRTStartup才调用main函数。
mainCRTStartup:C Runtimeup Code

如何在main()函数之前执行一些代码:

GCC

gcc中使用attribute关键字,声明constructor和destructor函数

__attribute() void before_main(){}

VC

vc中不支持attribute,可插入函数进入初始化函数列表[__xi_a,__xi_z](c)[__xc_a,__xc_z](c++)由CRTInit调用

  • #pragma data_seg(“.CRT$XIU”)
  • static func *before1[]={before_main1};

C++:

  • 可以A a,全局变量构造函数在mian之前
  • int g_iValue=func();

C的代码举例

// c_premain.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>

int before_main1()  
{  
    printf("before_main1()\n");  

    return 0;  
}  
int before_main2()  
{  
    printf("before_main2()\n");  

    return 0;  
}  
int after_main()
{
    printf("after_main()\n");
    return 0;
}
/*
__CRTInit中做一些初始化工作:
包括C库、C的初始化函数,C++库、C++的初始化函数等。
C和C++分别有一张表来保存初始化函数指针,
每个表又使用2个指针来明确范围,
__CRTInit会依次调用这2个表中的函数。
C初始化函数表:[ __xi_a, __xi_z]
C++初始化函数表: [ __xc_a, __xc_z]
现在对照代码注释,就会明白上述那段代码的作用。
通过特殊的段名称“.CRT$XIU”,“.CRT$XCU”,
链接器会把before1表放在“C初始化函数表”中,类似这样
[__xi_a, ..., before1(xiu), ..., __xi_z].
同理,before2表会被链接器放在“C++初始化函数表”中,象这样
[__xc_a, ..., before2(xcu), ..., __xc_z],
*/

typedef int func();  

#pragma data_seg(".CRT$XIU")  //用#pragma data_seg建立一个新的数据段并定义共享数据
static func * before1[] = { before_main1 };  

#pragma data_seg(".CRT$XCU")  
static func * before2[] = { before_main2 };  

#pragma data_seg() 

int _tmain(int argc, _TCHAR* argv[])
{
    _onexit(after_main);
    printf("hello world\n");

    return 0;
}

C++的代码举例

// cpp_premain.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
using std::cout;

int func()
{
    cout<<"func() called before main()"<<endl;
    return 100;
}
class A
{
public:
    A()
    {
        cout<<"A() constructor called"<<endl;
    }
    ~A()
    {
        cout<<"~A() destructor called"<<endl;
    }
};

A a;

int g_iValue = func();


int _tmain(int argc, _TCHAR* argv[])
{
    cout<<"main() called"<<endl;
    return 0;
}

Linux下代码举例

#include<stdio.h> 

__attribute__((constructor)) void before_main() { 
   printf("before main\n"); 
} 

__attribute__((destructor)) void after_main() { 
   printf("after main\n"); 
} 

int main(int argc, char **argv) { 
   printf("in main\n"); 
   return 0; 
}

猜你喜欢

转载自blog.csdn.net/youyou519/article/details/82703448