C언어 header/dlfcn.h
dlclose(3) - dynamic library unload
자연&사람
2019. 10. 1. 22:13
반응형
dlclose(3)
#include <dlfcn.h>
int dlclose(void *handle);
dlclose(3)함수는 dlopen(3)에 의해서 로딩된 shared object의 참조 count를 1 감소 시킵니다. 또한 전체 참조 count가 0이면 메모리에서 unload됩니다.
링크시 -ldl 추가 필요
파라미터
handle
- dlopen(3)에 의해서 return된 handle값
RETURN
0
- 정상적으로 처리하였습니다.
0 이외
- 오류가 발생하였습니다.
활용 예제
Sample). cos()함수 호출 예제
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(int argc, char **argv)
{
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen("libm.so", RTLD_LAZY);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror(); /* Clear any existing error */
/* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
would seem more natural, but the C99 standard leaves
casting from "void *" to a function pointer undefined.
The assignment used below is the POSIX.1-2003 (Technical
Corrigendum 1) workaround; see the Rationale for the
POSIX specification of dlsym(). */
*(void **) (&cosine) = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
printf("%f\n", (*cosine)(2.0));
dlclose(handle);
return 0;
}
see also: Shared Library 관련 API
반응형