2010-04-14 6 views
1

ENV를 찾을 수 없습니다 : GCC 버전 4.4.1 (우분투 4.4.1-4ubuntu9)동적 LIB 정적 lib 디렉토리를

응용 프로그램 : 빈은 (주) 동적 LIB (testb.so)를 호출하고, testb.so는 포함 정적 lib (libtesta.a).

파일 목록 :

후 컴파일 main.c를 test.h 교류 기원전 :

gcc -o testa.o -c a.c 

ar -r libtesta.a testa.o 

gcc -shared -fPIC -o testb.so b.c 

gcc -o main main.c -L. -ltesta -ldl 

는 성공을 컴파일하지만, 오류 실행 :

./main: symbol lookup error: ./testb.so: undefined symbol: print 

을 다음과 같이 코드 :

,210

test.h

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <dlfcn.h> 



int printa(const char *msg); 

int printb(const char *msg); 

교류

#include "test.h" 

int 
printa(const char *msg) 
{ 
    printf("\tin printa\n"); 
    printf("\t%s\n", msg); 
} 

BC

#include "test.h" 

int 
printb(const char *msg) 
{ 
    printf("in printb\n"); 
    printa("called by printb\n"); 
    printf("%s\n", msg); 
} 

main.c를

정적 컴파일 동안은 GCC에 -fPIC 추가하는 경우
#include "test.h" 


int 
main(int argc, char **argv) 
{ 
    void *handle; 
    int (*dfn)(const char *); 

    printf("before dlopen\n"); 

    handle = dlopen("./testb.so", RTLD_LOCAL | RTLD_LAZY); 
    printf("after dlopen\n"); 
    if (handle == NULL) { 
     printf("dlopen fail: [%d][%s][%s]\n", \ 
      errno, strerror(errno), dlerror()); 
     exit(EXIT_FAILURE); 
    } 

    printf("before dlsym\n"); 
    dfn = dlsym(handle, "printb"); 
    printf("after dlsym\n"); 
    if (dfn == NULL) { 
     printf("dlsym fail: [%d][%s][%s]\n", \ 
      errno, strerror(errno), dlerror()); 
     exit(EXIT_FAILURE); 
    } 

    printf("before dfn\n"); 

    dfn("printb func\n"); 

    printf("after dfn\n"); 

    exit(EXIT_SUCCESS); 
} 
+3

오류 메시지가 소스 코드와 일치하지 않습니다. 오류 메시지는 소스에서 사용되지 않는 기호'print'를 나타냅니다. – qrdl

답변

0

, 그러면 잘 작동합니다.

관련 문제