我遇到以下問題。我寫了一個共享庫共享庫構造函數未執行
#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