before main

Posted by shensunbo on January 15, 2024
  1. 不要使用std::cout, 在main函数开始之前cout可能未初始化完成,导致段错误
  2. __attribute__((constructor)) 可以存在多个,同一文件中,按声明顺序, 函数执行顺序为:从上到下。在不同文件中,执行顺序不确定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdio>
// before main test demo
// for GCC

static int i = 0;

__attribute__((constructor)) void beforeMain() {
    i++;
    printf("Before main() - __attribute__((constructor)), i %d\n", i);
    return; 
}

__attribute__((destructor)) void afterMain() {
    printf("After main() - __attribute__((destructor))\n");
    return; 
}

int main(int argc, char *argv[])
{
    std::cout << "Hello, World! i: " << i << std::endl;
    return 0;
}