__attribute__((constructor))

来源:互联网 发布:分水岭算法示意图 编辑:程序博客网 时间:2024/06/06 20:06

gcc为函数提供了几种类型的属性,其中包含:构造函数(constructors)和析构函数(destructors)。
程序员应当使用类似下面的方式来指定这些属性:

__attribute__((constructor)) // 在main函数被调用之前调用__attribute__((destructor)) // 在main函数被调用之后调
#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; }
//这个例子的输出结果将会是:before mainin mainafter main
2 0
原创粉丝点击