共享库构造函数不执行 [英] Shared Library Constructor is not executed

查看:194
本文介绍了共享库构造函数不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我写了一个共享库

  #include< stdio.h> 
#include< stdlib.h>

static void __attribute__((constructor))test_init(void);
static void __attribute__((destructor))test_clean(void);

/ *初始化* /
static void test_init(void){
fprintf(stderr,initialized'\
);
fflush(stderr);
}
/ * CleanUp * /
static void test_clean(void){
fprintf(stderr,cleared up \\\
);
fflush(stderr);
}

double test(double x){
return 2.0 * x;
}

并使用

编译

gcc -c -fPIC testlib.c -o testlib.o



ld -shared -o libtest.so testlib.o



然后将其包含到测试程序中

  #include< stdio.h> 
#include< stdlib.h>
extern double test(double x);
void main(void){

printf(%。10e\\\
,test(10.0));
}

我编译并开始使用



gcc testprog.c -o testprog -L。 -ltest



LD_LIBRARY_PATH =。 ./testprog



然后输出结果为



2.0000000000e + 01



这意味着构造函数/析构函数不会被执行。另一方面,如果我编译



ar rvs testlib.a testlib.o



gcc testprog.c testlib.a -o testprog



程序的输出由



testprog
初始化
2.0000000000e + 01
清除



为什么动态链接库时不会执行构造函数?



我使用以下版本



GNU ld(GNU Binutils; openSUSE 11.3)2.20.0.20100122-6
gcc 4.5.0版20100604 [gcc-4_5-branch revision 160292](SUSE Linux)



预先感谢您的帮助!



编辑:2011-04-13,11:05



非常感谢您,



文件间接帮助!神奇的提示是,应该通过编译器引用链接器...


gcc -fPIC testlib.c -shared
-Wl,-soname,libtest.so -o libtest.so


工程!!!



此文本仅供参考,不过为了方便起见, >我不是该领域的专家,但Google快速搜索给了我 this 。只读文档的开头,如果我得到它的正确的问题是这样:



链接静态你的程序在执行时是自包含的...它有



在程序执行时调用库函数时,链接器会动态链接器尝试解析所有内容未解析的函数引用,通过查看它是否在一些库中有一个实现。如果这样,它加载这个实现,即只是函数代码。



所以如果我得到这个权利,动态链接器只加载库的一部分,即所需的函数,而不是整个库然后这将解释为什么你的构造函数不被调用时,你的库动态链接。


I have the following problem. I write a shared library

#include <stdio.h>
#include <stdlib.h>

static void __attribute__ ((constructor)) test_init(void);
static void __attribute__ ((destructor))  test_clean(void);

/*  Initialization  */
static void     test_init(void){
        fprintf(stderr,"initialized\n");
        fflush(stderr);
}
/*  CleanUp */
static void test_clean(void){
        fprintf(stderr,"cleaned up\n");
        fflush(stderr);
}

double  test (double x){
    return  2.0*x;
}

And compile it using

gcc -c -fPIC testlib.c -o testlib.o

ld -shared -o libtest.so testlib.o

Then I include it into a test program

#include <stdio.h>
#include <stdlib.h>
extern double   test(double x);
void    main(void){

    printf("%.10e\n",test(10.0));
}

which I compile and start using

gcc testprog.c -o testprog -L. -ltest

LD_LIBRARY_PATH=. ./testprog

Then the output is given by

2.0000000000e+01

which means that the constructor/destructor are not executed. On the other hand, if I compile

ar rvs testlib.a testlib.o

gcc testprog.c testlib.a -o testprog

the output of the program is given by

testprog initialized 2.0000000000e+01 cleaned up

Why are the constructors not executed if the library is linked dynamically?

I use the following versions

GNU ld (GNU Binutils; openSUSE 11.3) 2.20.0.20100122-6 gcc version 4.5.0 20100604 [gcc-4_5-branch revision 160292] (SUSE Linux)

Thank you in advance for your help!

Edited: 2011-04-13, 11:05

Thank you very much luxifer,

the document helped indirectly! The magic hint was that one should involve the linker through the compiler...

gcc -fPIC testlib.c -shared -Wl,-soname,libtest.so -o libtest.so

works!!!

解决方案

This text is meant for reference, but I'm coming over to your office for convenience :)

I'm not an expert on that field but a quick Google search gave me this. Reading just the beginning of the document and if I get it right the problem is this:

Linked statically your program is self-contained at execution time... it has the whole library in it and it's completely loaded into memory when you run it.

Linked dynamically when the library function is called from your program at execution time the linker tries to resolve all unresolved references on functions by looking if it has an implementation in some library. If so it loads this implementation, i.e. just the functions code.

So if I get this right and the dynamic linker just loads portions of libraries, i.e. needed functions, and not the whole library then this would explain why your constructor isn't called when your library is linked dynamically.

这篇关于共享库构造函数不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆