srupのメモ帳

競プロで解いた問題や勉強したことを記録していくメモ帳

main()の前後で関数を呼び出す

GCC拡張機能を使って行っています.

#include <stdio.h>

__attribute__((constructor))
void foo() {
    printf("hello, before main\n");
}

__attribute__((destructor))
void bar() {
    printf("hello, after main\n");
}

int main(int argc, char const *argv[]) {
    printf("hello, world\n");
    return 0;
}
$ gcc test.c
$ ./a.out
hello, before main
hello, world
hello, after main