함수명 문자열로 함수 호출해 보기
-------------------------------------------
1. "hello" 함수를 호출하는 예제
-------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
/* 함수포인터 정의 */
typedef void (*t_func)(char*);
void hello(char*);
int main()
{
/* 동적 라이브러리에 함수가 있을 경우엔 NULL대신 라이브러리 파일명을 넣어 주면 됨. */
void *slib = dlopen(NULL,RTLD_NOW);
/* 함수포인터 변수 정의*/
t_func myFunc;
/* 문자열로부터 함수 포인터 받기 */
myFunc = (t_func)dlsym(slib, "hello");
/* 함수 호출. 즉 함수명은 원래 포인터임. */
myFunc("dev4u");
dlclose(slib);
return 1;
}
void hello(char* name)
{
printf("hello %s ! \n", name);
}
/*** EOF ***/
-------------------------------------------
2. "printf" 함수를 호출하는 예제
-------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
typedef int (*t_print)(const char*,...);
int main()
{
void *slib = dlopen(NULL,RTLD_NOW);
t_print myPrint;
myPrint = (t_print)dlsym(slib, "printf");
myPrint("Hello %s !!!\nn", "dev4u");
dlclose(slib);
return 1;
}
/*** EOF ***/