dlopen() 후 callback함수 사용하기
글쓴이: papablue / 작성시간: 수, 2009/06/10 - 10:08오전
기존에는 dlopen을 이용하지 않다가 library loading 시간이 문제가 되어
dlopen으로 바꾸려고 하는데요 (dlopen으로 변경하기 전에는 문제없음)
main program에 정의해 놓은 callback함수를 library에서 호출 하는 방법이 있는지 알고 싶습니다.
아래는 대략화 한 내용입니다.
[A program] - C
void A_callback() { print("called"); } typedef int(*abc_t)(char *); void A_main() { void* handle; int res; abc_t abc; /* Open a shared library. */ handle = dlopen( "libBLibrary.so.1", RTLD_LAZY ); abc = (abc_t)dlsym(handle, "main"); res = abc("call lib"); dlclose(handle); }
[BLibrary program] - C++
extern "C" void A_callback(); void B_main(char *) { A_callback(); }
library가 로딩되자마자
unknown symbol: A_callback
이 나오는 문제입니다.
Forums:
음..
binary 와 shared library 가 cross-refrence 할 수 없습니다.
A_callback 모듈은 BLibrary 모듈로 보내시거나..
별도의 object 로 빼서, BLibrary 에 함께 링크되도록 하셔야 합니다.
그리고, library 에 "main" 이라는 이름은 적당하지 않은 것 같습니다.
다음과 같은 구조면 적당하지 않을까 합니다.
BLibrary
B_main.o : A_callback_wrapper
A_callback.o : A_callback
AProgram
A_main.o : dlopen(BLibrary)/dlsym(A_callback or A_callback_wrapper)/dlclose
(..추가..)
본래의 구조를 유지해야만 하는 상황이라면..
function pointer 를 넘기도록 하면 될 것 같습니다.
(다만, 유지 보수 및 디버깅이 복잡해 질 가능성이 높습니다.)
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
댓글 달기