2009-11-17 6 views
1

공유 라이브러리 "libwiston.so"가 있습니다. 다른 프로젝트에서 사용하는 "libAnimation.so"라는 또 다른 공유 라이브러리를 만드는 데이 방법을 사용하고 있습니다. 이제 두 번째 라이브러리 인 "libAnimation.so"를 테스트 코드에서 올바르게 사용할 수 없습니다. 그래서 두 번째 라이브러리 "libAnimation.so"의 생성이 옳은지 의심 스럽습니다. 이 lib를 만드는 gcc 명령은다른 공유 라이브러리를 사용하여 공유 라이브러리 만들기

g++ -g -shared -Wl,-soname,libwiston.so -o libAnimation.so $(objs) -lc". 

누군가가이 문제를 겪고 있습니까?

답변

3

괴상한 링크 줄처럼 보입니다. libAnimation.so을 만들고 있지만 DT_SONAME 내부 이름은 libwiston.so입니다.

나는 당신이하고 싶은 것을 생각하지 않습니다. libAnimation.solibwiston.so (-lwiston)와 연결하고 싶지 않으십니까?

g++ -g -shared -o libAnimation.so $(objs) -lc -lwiston 

automake/autoconf에서 빌드를 래핑하고 libtool을 사용하여 공유 라이브러리를 올바르게 작성하는 것이 더 쉽다고 생각합니다.

+0

대단히 감사합니다.이 명령을 테스트했지만 automake/autoconf를 사용하여이 공유 라이브러리를 만든다. – mpouse

+0

Makefile.am의 libAnimation에 대해 @mpouse가 libwiston 항목을 검사하는지 확인한다. –

0

공유 라이브러리를 만드는 과정에 대해 겸손한 검토를하겠습니다.

libwiston.so을 만들어 보겠습니다. 먼저 우리가 내보내려는 기능을 구현 한 다음 헤더에 정의하여 다른 프로그램에서 호출하는 방법을 알고 있습니다.

/* file libwiston.cpp 
* Implementation of hello_wiston(), called by libAnimation.so 
*/ 
#include "libwiston.h" 

#include <iostream> 

int hello_wiston(std::string& msg) 
{ 
    std::cout << msg << std::endl; 

    return 0; 
} 

과 :

/* file libwiston.h 
* Exports hello_wiston() as a C symbol. 
*/ 
#include <string> 

extern "C" { 
int hello_wiston(std::string& msg); 
}; 

이 코드는 컴파일 할 수 있습니다 : g++ libwiston.cpp -o libwiston.so -shared

이제 우리는 첫 번째로 내 보낸 함수를 호출 libAnimation.so라는 이름의 두 번째 공유 라이브러리를 구현

도서관.

/* file libAnimation.cpp 
* Implementation of call_wiston(). 
* This function is a simple wrapper around hello_wiston(). 
*/ 
#include "libAnimation.h" 
#include "libwiston.h" 

#include <iostream> 

int call_wiston(std::string& param) 
{ 
    hello_wiston(param); 

    return 0; 
} 

와 헤더 :

/* file libAnimation.h 
* Exports call_wiston() as a C symbol. 
*/ 
#include <string> 

extern "C" { 
int call_wiston(std::string& param); 
}; 

가 함께 컴파일 g++ libAnimation.cpp -o libAnimation.so -shared -L. -lwiston

마지막으로, 우리는 libAnimation을 테스트 할 수있는 작은 응용 프로그램을 만듭니다.

/* file demo.cpp 
* Implementation of the test application. 
*/ 
#include "libAnimation.h" 
int main() 
{ 
    std::string msg = "hello stackoverflow!"; 
    call_wiston(msg); 
} 

그리고 그것을 컴파일 : g++ demo.cpp -o demo -L. -lAnimation 당신이 공유 라이브러리에서 내 보낸 문자를 나열하는 데 사용할 수있는 나노라는 흥미로운 도구가있다

.이 예제를 사용하여 기호를 확인하기 위해 다음 명령을 실행할 수 있습니다 :

nm libAnimation.so | grep call_wiston 

출력 : 또한

00000634 t _GLOBAL__I_call_wiston 
000005dc T call_wiston 

과 :

nm libwiston.so | grep hello_wiston 

출력 :

0000076c t _GLOBAL__I_hello_wiston 
000006fc T hello_wiston 
+0

내가 링크하지 않으면 정의되지 않은 'hello_wiston'참조와 함께 애플리케이션 링크가 실패 할 것이다. 'libwiston.so'. libwiston.a가 있으면 작동 할 것입니다. –

+0

@Dmitry 연결이 성공하고 데모가 작동합니다! libwiston.so와 링크되어야하는 유일한 바이너리는 libAnimation이며 완료되었습니다. – karlphillip

+0

테스트 해본 시스템은 무엇입니까? 데비안 gcc-4.4.5에서 ld-2.20.1은 내가 언급 한 것처럼 ./libAnimation.so :'hello_wiston '에 대한 정의되지 않은 참조를 제공합니다. –

관련 문제