欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言中怎么在main函數(shù)開始前執(zhí)行函數(shù)

 更新時間:2013年10月09日 09:27:46   作者:  
C語言中怎么在main函數(shù)開始前執(zhí)行函數(shù)呢?下面小編就大家詳細(xì)的介紹一下。需要的朋友可以過來參考下,希望對大家有所幫助

在gcc中,可以使用attribute關(guān)鍵字,聲明constructor和destructor,代碼如下:

復(fù)制代碼 代碼如下:

#include <stdio.h>

__attribute((constructor)) void before_main()
{
 printf("%s/n",__FUNCTION__);
}

__attribute((destructor)) void after_main()
{
 printf("%s/n",__FUNCTION__);
}

int main( int argc, char ** argv )
{
 printf("%s/n",__FUNCTION__);
 return 0;
}


 vc不支持attribute關(guān)鍵字,在vc中,可以使用如下方法:
復(fù)制代碼 代碼如下:

#include <stdio.h>

int
main( int argc, char ** argv )
{
        printf("%s/n",__FUNCTION__);

        return 0;
}


int before_main()
{
        printf("%s/n",__FUNCTION__);

        return 0;
}

int after_main()
{
        printf("%s/n",__FUNCTION__);

        return 0;
}

typedef int func();

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

#pragma data_seg(".CRT$XPU")
static func * after[] = { after_main };

#pragma data_seg()


編譯執(zhí)行,上述兩段代碼的結(jié)果均為:

before_main

main

after_main

可以在main前后調(diào)用多個函數(shù),在gcc下使用attribute聲明多個constructor、destructor,vc下在before、after數(shù)組中添加多個函數(shù)指針。

相關(guān)文章

最新評論