공유 라이브러리를 만드는 과정에 대해 겸손한 검토를하겠습니다.
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
대단히 감사합니다.이 명령을 테스트했지만 automake/autoconf를 사용하여이 공유 라이브러리를 만든다. – mpouse
Makefile.am의 libAnimation에 대해 @mpouse가 libwiston 항목을 검사하는지 확인한다. –