我想构建一些代码,在加载共享库时调用一些代码。我以为我会这样做:
#pragma init(my_init)
static void my_init () {
//do-something
}
int add (int a,int b) {
return a+b;
}
所以当我用
gcc -fPIC -g -c -Wall tt.c
它返回
gcc -fPIC -g -c -Wall tt.c
tt.c:2: warning: ignoring #pragma init
tt.c:4: warning: ‘my_init’ defined but not used
所以它忽略了我的#pragmas。我在实际代码中尝试了这个,我的代码中止了,因为在 pragma 部分中没有调用一个函数,因为它被忽略了。
我如何让 gcc 使用这些 #pragma init 和 fini 语句?