#include #ifdef _MSC_VER #ifdef __cplusplus #define CONSTRUCTOR_FUNC(func) \ struct func##_constructor { \ func##_constructor() { func(); } \ }; \ static func##_constructor func##_instance; #else #pragma section(".CRT$XCU", read) #define CONSTRUCTOR_FUNC(func) \ __declspec(allocate(".CRT$XCU")) void (*p_init_func)() = func; #endif #else #define CONSTRUCTOR_FUNC(func) \ static void func() __attribute__((constructor)); #define DESTRUCTOR_FUNC(func) \ static void func() __attribute__((destructor)); #endif static void my_constructor(void) { printf("Constructor called before main()\n"); } CONSTRUCTOR_FUNC(my_constructor); int main() { printf("Hello from main!\n"); return 0; }