2013-05-10 102 views
0

我試圖調用從共享庫中的函數「核心」功能,但我得到:從共享庫中調用函數

./a.out: symbol lookup error: ./libtest.so: undefined symbol: testf 

我正在使用的代碼是非常基本的,因爲我只是進入寫作共享庫,它只是用於測試目的: main.h

extern void testf(); 

的main.c

#include <stdio.h> 
#include <dlfcn.h> 

extern void testf() 
{ 
    printf("bla bla\n"); 
} 

int main() { 

void *handle = NULL; 
void (*testlib)(void) = NULL; 

handle = dlopen("./libtest.so" ,RTLD_LAZY); 
testlib = dlsym(handle, "testfunc"); 

if (testlib == NULL) 
{ 
    printf("Error: %s \n", dlerror()); 
} 
else 
{ 
    testlib(); 
} 
} 

libtest.c

#include <stdio.h> 
#include "main.h" 

void testfunc() { 

printf("Test plugin\n"); 
testf(); 
} 

而且命令我編譯:

gcc -fPIC -g -c -Wall libtest.c 
gcc -shared -Wl,-soname,libtest.so.1 -o libtest.so libtest.o -lc 
gcc main.c -ldl 

是否有可能實現這一目標?試圖找到答案,但不知道如何形成正確的問題,所以我可以更好地搜索它。

謝謝!

回答

0

對不起,好不容易纔找到了答案: 我編譯與錯誤的參數主要程序,應該使用:

gcc main.c -ldl -rdynamic 
0

這裏您試圖從庫中調用一個可執行文件的函數。我想你實際上需要相反的。

+0

我試圖從庫中調用函數testf()和我想要的testf()函數被定義在調用它的庫之外。 – user1667175 2013-05-10 15:04:22

+0

然後去另一個圖書館 – akhil 2013-05-10 15:11:53