gcc __attribute__关键字举例之constructor/destructor

本文介绍了如何使用构造 (__init__) 和析构 (__exit__) 函数来实现程序开始和结束时的特定操作。通过 gcc/g++ 的 attribute 扩展,可以在 C/C++ 程序启动前和结束后自动调用这些函数。

constructor属性设置函数在所有函数之前执行,destructor设置函数在所有函数退出后执行。

test.cc

#include <stdio.h>
void __init()
{
  printf("before main\n");
}

void __exit()
{
  printf("after main\n");
}

void __init() __attribute__ ((constructor));
void __exit() __attribute__ ((destructor));
int main()
{
  printf("in main\n");
  return 0;
}
编译运行:
$ g++ -o test constructor.cc ;./test 
before main
in main
after main

或者

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

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

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

编译运行:
$ g++ -o test constructor.cc ;./test 
before main
in main
after main


 



-- Performing Test HAVE_GCC_THREAD_LOCAL_STORAGE -- Performing Test HAVE_GCC_THREAD_LOCAL_STORAGE - Success -- Performing Test HAVE_MSC_THREAD_LOCAL_STORAGE -- Performing Test HAVE_MSC_THREAD_LOCAL_STORAGE - Failed -- Performing Test HAVE_CONSTRUCTOR_ATTRIBUTE -- Performing Test HAVE_CONSTRUCTOR_ATTRIBUTE - Failed -- Performing Test HAVE_DESTRUCTOR_ATTRIBUTE -- Performing Test HAVE_DESTRUCTOR_ATTRIBUTE - Failed -- Performing Test HAVE_FALLTHROUGH_ATTRIBUTE -- Performing Test HAVE_FALLTHROUGH_ATTRIBUTE - Failed -- Performing Test HAVE_GCC_VOLATILE_MEMORY_PROTECTION -- Performing Test HAVE_GCC_VOLATILE_MEMORY_PROTECTION - Failed -- Performing Test HAVE_GCC_NARG_MACRO -- Performing Test HAVE_GCC_NARG_MACRO - Failed -- Performing Test HAVE_COMPILER__FUNC__ -- Performing Test HAVE_COMPILER__FUNC__ - Failed -- Performing Test HAVE_COMPILER__FUNCTION__ -- Performing Test HAVE_COMPILER__FUNCTION__ - Failed -- Performing Test HAVE_GCC_BOUNDED_ATTRIBUTE -- Performing Test HAVE_GCC_BOUNDED_ATTRIBUTE - Failed -- Performing Test HAVE_LD_VERSION_SCRIPT -- Performing Test HAVE_LD_VERSION_SCRIPT - Failed -- Check if the system is big endian -- Searching 16 bit integer -- Looking for sys/types.h -- Looking for sys/types.h - not found -- Looking for stddef.h -- Looking for stddef.h - not found -- Check size of unsigned short -- Check size of unsigned short - failed -- Check size of unsigned int -- Check size of unsigned int - failed -- Check size of unsigned long -- Check size of unsigned long - failed CMake Error at /usr/share/cmake-3.5/Modules/TestBigEndian.cmake:51 (message): no suitable type found Call Stack (most recent call first): ConfigureChecks.cmake:417 (test_big_endian) CMakeLists.txt:98 (include) -- Configuring incomplete, errors occurred! See also "/home/bba/work/BBA1_5/MBB_Platform/apps/public/libssh/build/CMakeFiles/CMakeOutput.log". See also "/home/bba/work/BBA1_5/MBB_Platform/apps/public/libssh/build/CMakeFiles/CMakeError.log". make/apps/pubapps/libssh.mk:9: recipe for target 'libssh' failed make: *** [libssh] Error 1
最新发布
10-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值