#include<iostream>
#define SECNAME ".CRT$XCG"
#pragma section(SECNAME,long,read)
void foo() {
std::cout << "hello" << '\n';
}
typedef void(__cdecl* _PVFV)();
__declspec(allocate(SECNAME)) _PVFV dummy[] = { foo };
int main() {
std::cout << "main" << '\n';
}
foo函数将先于main函数执行,也就是自己添加初始化函数,如果在GCC可以直接使用 __attribute__((constructor))声明函数就好
参考自《程序员的自我修养_链接_装载与库.pdf》11.4全局构造与析构