我是靠谱客的博主 辛勤画笔,这篇文章主要介绍关于GCC的 __attribute__ ((constructor)),现在分享给大家,希望可以做个参考。

gcc为函数提供了几种类型的属性,其中包含:

构造函数(constructors)析构函数(destructors)
程序员应当使用类似下面的方式来指定这些属性:
static void start(void) __attribute__ ((constructor));
static void stop(void) __attribute__ ((destructor));
带有"构造函数"属性的函数将在main()函数之前被执行,
而声明为"析构函数"属性的函数则将在main()退出时执行。

示例代码

vi test.cpp

#include<stdio.h>
__attribute__((constructor)) void before_main()
{
   printf("before main n");
}

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

int main()
{
   printf("in main n");
   return 0;
}

编译:
g++ test.cpp

运行:
./a.out
在这里插入图片描述

最后

以上就是辛勤画笔最近收集整理的关于关于GCC的 __attribute__ ((constructor))的全部内容,更多相关关于GCC的内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(124)

评论列表共有 0 条评论

立即
投稿
返回
顶部