node中的初始化宏定义

gcc中函数声明后面追加__attribute__ ((constructor))

vc中 利用CRT 中的.CRT$XCU

这样可以在程序入口之前执行一些数据初始化代码

#if defined(_MSC_VER)
#pragma section(".CRT$XCU", read)
#define NODE_C_CTOR(fn)                                               \
  static void __cdecl fn(void);                                       \
  __declspec(dllexport, allocate(".CRT$XCU"))                         \
      void (__cdecl*fn ## _)(void) = fn;                              \
  static void __cdecl fn(void)
#else
#define NODE_C_CTOR(fn)                                               \
  static void fn(void) __attribute__((constructor));                  \
  static void fn(void)
#endif

demo

NODE_C_CTOR(InitTestFunc){
	printf("before main function");
}