2024-04-24 13:31:59 +00:00
|
|
|
// this specific section contains before-main function pointers
|
|
|
|
// this pragma ensures the compilation unit will generate this section
|
|
|
|
#pragma section(".CRT$XCU", read)
|
|
|
|
|
|
|
|
// declare the "before main" function
|
|
|
|
static void run_before_main_func(void);
|
|
|
|
|
|
|
|
// set the before-main execution function pointer
|
|
|
|
__declspec(allocate(".CRT$XCU"))
|
2024-04-24 23:28:39 +00:00
|
|
|
__pragma(comment(linker,"/include:run_before_main_ptr"))
|
2024-04-24 13:31:59 +00:00
|
|
|
void (*run_before_main_ptr)(void) = run_before_main_func;
|
|
|
|
|
|
|
|
// define the "before main" function
|
|
|
|
int x = 0;
|
|
|
|
static void run_before_main_func(void){
|
|
|
|
x = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
// main
|
|
|
|
#include <stdio.h>
|
|
|
|
int main(){
|
|
|
|
printf("x = %d\n", x);
|
|
|
|
return(0);
|
2024-04-24 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// wrapped in a macro:
|
|
|
|
|
2024-04-24 23:28:39 +00:00
|
|
|
#define BEFORE_MAIN(n) static void n(void); \
|
|
|
|
__declspec(allocate(".CRT$XCU")) \
|
|
|
|
__pragma(comment(linker,"/include:" #n "__")) \
|
|
|
|
void (*n##__)(void) = n; \
|
2024-04-24 19:04:52 +00:00
|
|
|
static void n(void)
|
|
|
|
|
|
|
|
BEFORE_MAIN(before_main_rule){
|
|
|
|
// do work here
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|