我遇到以下问题。我写了一个共享库共享库构造函数未执行
#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;
}
而且使用
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\n",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-分支修订160292](SUSE Linux)的
预先感谢您的帮助!
编辑:2011-04-13,11:05
非常感谢您luxifer,
文档间接地帮助了!神奇的暗示是一个应该包括通过编译链接...
的gcc -fPIC -shared testlib.c 轮候册,-soname,libtest.so -o libtest.so
作品!
main()的返回类型为int。总是。 – Thomas 2012-04-13 21:26:46