ใช้ GCC Attribute กำหนดฟังก์ชันรันก่อน/หลัง main:
__attribute__((constructor)): รันก่อน main()__attribute__((destructor)): รันหลัง main()
ตัวอย่าง
#include <stdio.h>
void before_main() __attribute__((constructor));
void after_main() __attribute__((destructor));
void before_main() { printf("Before main\n"); }
void after_main() { printf("After main"); }
int main() { printf("Inside main\n"); }
ผลลัพธ์
Before main Inside main After main
ใช้สำหรับ Startup/Cleanup Tasks