How to execute code before and after main function
Date: 2024-01-05Last modified: 2024-11-02
Table of contents
#include <stdio.h>
#include <stdlib.h>
void my_before_main(void) __attribute__((constructor));
void my_after_main(void) __attribute__((destructor));
void my_before_main_2(void) __attribute__((constructor));
void my_after_main_2(void) __attribute__((destructor(119)));
__attribute__((destructor(120))) void my_after_main_3(void) {
printf("start after main 3\n");
return;
}
// BOth attributes can be specified on the same function
void __attribute__((constructor)) __attribute__((destructor))
another_before_and_after_main() {
printf("%s called before and after main()\n", __PRETTY_FUNCTION__);
}
void my_before_main(void) {
printf("start before main\n");
return;
}
void my_after_main(void) {
printf("start after main\n");
return;
}
void my_before_main_2(void) {
printf("start before main 2\n");
return;
}
void my_after_main_2(void) {
printf("start after main 2\n");
return;
}
int main(int argc, char *argv[]) {
printf("start in main\n");
return 0;
}