유닉스 에서는 공유 라이브러리(shared library) 혹은 shared Object (공유 객체)를 컴파일이 아닌


실행 시간에 동적으로 메모리에 올려서 사용할 수 있다.  이것을 하기 위해 도와주는것이


dlopen, dlsym, dlclose가 있다.



우선 dlopen


void *dlopen(const char *filename,int flag);


const char * filename : 공유 라이브러리 파일명을 의미하며 전체 경로 혹은 LD_LIBRARY_PATH 에 분리된 디렉토리중 에 존재하여야 한다.


int flag :

-> RTLD_LAZY : 실제로 사용되는 시점에 불러옴 ( dlsym 이 호출되어 함수등을 바인한 시점이 되었을때)

-> RTLD_NOW : dlopen 함수내에서 로딩을 끝냄


return values : 정상적으로 공유 라이브러리가 로딩되면 NULL 이 아닌 handle이 반환된다.

비정상적인 경우에 NULL이 반환 


dlopen 함수는 정상적인 호출시마다 공유 라이브러리에 대한 참조 count를 1증가 시킨다. 0 ~ 1로 증가할때애 메모리에 로딩 되어진다.


void *handle = NULL;

handle = dlopen("libmouse.so",RTLD_LAZY);


if(handle==NULL){

printf("%s Error -> Number is  :%d\n", errno);

return 0;

}


void *dlsym(void *handle, const char *symbol);


void *handle : dlopen 이 반환한 핸들 값


const char *symbol : 찾으려는 symbol(함수명)


return values : symbol 에 대한 위치 (Pointer)



공유 라이브러리에 해당 함수가



int init(mouse_t *mouse, int a, char **b) 로 선언이 되어있다면


사용해야되는 곳에서 받는 방법은


int (*init)(mouse_t *,int,char**) 로 바꿔서 쓴다.


원래 함수에서


함수명을 *로 묶는다.


int (*init)(mouse_t *mouse,int a, char **b)

이 후에 -> 해당 가변인자들을 없앤다.


int (*init)(mouse_t, int, char** )


이렇게 선언하고 


init = init로 받으면댐 ! 냐하핳


Example


void handler(mouse_t* mouse)


를 참조할 때에는


void(*handler)(mouse_t *)












dlclose는 아직 나중에 








'GNU > FreeBSD' 카테고리의 다른 글

종료  (0) 2016.07.20
마우스 컨트롤(mousesystem.c)  (0) 2016.07.08
마웅스 컨트롤(ums)  (0) 2016.07.08
마우스 컨트롤(sysmouse)  (0) 2016.07.08
마우스 컨트롤(Command)  (0) 2016.07.08

+ Recent posts