#pragma init和#pragma fini在linux上使用gcc编译器 [英] #pragma init and #pragma fini using gcc compiler on linux
问题描述
我想构建一些调用共享库的loadup代码的代码。我想我会这样做:
#pragma init(my_init)
static void my_init (){
// do-something
}
int add(int a,int b){
return a + b;
$ / code>
所以当我用
gcc -fPIC -g -c -Wall tt.c
它返回
gcc -fPIC -g -c -Wall tt.c
tt.c:2:warning:ignoreoring# pragma init
tt.c:4:warning:'my_init'已定义但未使用
所以它忽略了我的#pragmas。我在真正的代码中尝试了这一点,并且我的代码中止了,因为函数没有在编译指示段中被调用,因为它被忽略了。
如何使用gcc来使用这些#pragma init和fini语句?
编译指示几乎都是特定于编译器的。 GCC没有实现 init
,但是您可以使用构造函数
函数属性获得相同的效果:
$ b $
static __attribute __((constructor))void my_init()
{
// do-something
}
还有一个对应的析构函数
属性。 / p>
I would like to build some code which calls some code on loadup of the shared library. I thought i would do it like this:
#pragma init(my_init)
static void my_init () {
//do-something
}
int add (int a,int b) {
return a+b;
}
So when i build that code with
gcc -fPIC -g -c -Wall tt.c
It returns
gcc -fPIC -g -c -Wall tt.c
tt.c:2: warning: ignoring #pragma init
tt.c:4: warning: ‘my_init’ defined but not used
So its ignoring my #pragmas. I tried this in real code and my code aborted because a function hadn't been called in the pragma section because it was ignored.
How do i get gcc to use these #pragma init and fini statements?
pragmas are almost all compiler-specific. GCC doesn't implement init
, but you can get the same effect using the constructor
function attribute:
static __attribute__((constructor)) void my_init()
{
//do-something
}
There's also a corresponding destructor
attribute.
这篇关于#pragma init和#pragma fini在linux上使用gcc编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!