리눅스에서는 Shared Object 라고 부르는 DLL을 로드해서 사용하는걸 플러그인이라구 불르는거 같더라능. 윈도우라면 DLL을 불러오면 될태고.....아무튼 플러그인과 메인 프로그램 사이의 인터페이스를 잘 정의하는게 관건있거 같더라능.
뭐...어짜피 난 잘모르구 -_-;;;; 리눅스에서 Shared Object를 불러다 쓰는건 dl 라이브러리를 사용하면 된다능. dlopen() 으로 .so를 연 다음, dlsym() 으로 써서 잘 사용하구 나서 dlclose() 를 해주면 된다능. 자세한 사용법은 man page를 보는게 훨 좋을태니 아래 링크를 눌르라능
헬로우 월드 문자열을 플러그인을 불러다 적당히 변형해서 출력하는 프로그램을 싸봤다능. 소스코드 전체를 올려놓을태니 궁금하면 이걸 받아서 보라능. make 한담에 아래 처럼 켜보면 된다능. downcase.so를 아규먼트로 넣어주면 소문자로 출력해준다능. 끵.
dmw@dmw-desktop:~/works/linux_plugin_example$ ./hello_plugin upcase.so
ORIGINAL MESSAGE : HelLO wOrLD
LOAD PLUG-IN ... success!
converting message using plug-in
CONVERTED MESSAGE : HELLO WORLD
ORIGINAL MESSAGE : HelLO wOrLD
LOAD PLUG-IN ... success!
converting message using plug-in
CONVERTED MESSAGE : HELLO WORLD
메인 프로그램은 아래처럼 생겼고....
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
typedef int(*p2func)(char*);
int open_plugin(void** plugin_handle, char* plugin_name);
int close_plugin(void* plugin_handle);
p2func import_function(void* plugin_handle, const char** error);
int main(int argc, char** argv) {
int res;
void* plugin_handle;
const char* error;
char hello_message[] = "HelLO wOrLD\n";
p2func converter;
if(argc == 1) {
printf("hello_plugin upcase.so or downcase.so\n");
return 1;
}
printf("ORIGINAL MESSAGE : %s\n", hello_message);
printf("LOAD PLUG-IN ... ");
res = open_plugin(&plugin_handle, argv[1]);
if(res) {
fprintf(stderr, "\nplug-in load error : %s\n", dlerror());
return 1;
}
converter = import_function(plugin_handle, &error);
if(error) {
fprintf(stderr, "\nplug-in load error : %s\n", error);
close_plugin(plugin_handle);
return 1;
} else {
printf("success!\n");
}
printf("\tconverting message using plug-in\n");
converter(hello_message);
printf("\nCONVERTED MESSAGE : %s\n", hello_message);
close_plugin(plugin_handle);
return 0;
}
int open_plugin(void** plugin_handle, char* plugin_name) {
*plugin_handle = dlopen(plugin_name, RTLD_NOW);
if(*plugin_handle == NULL) {
return 1;
}
return 0;
}
p2func import_function(void* plugin_handle, const char** error) {
p2func func;
// dlsym() is return NULL if there is no symbol in DL library.
// If symbol has a NULL as it's value. We can not determine calling dlsym()
// is success or not. So, We are using dlerror()
*error = dlerror(); // clear error code
func = dlsym(plugin_handle, "conv");
*error = dlerror();
if(*error) {
return NULL;
}
return func;
}
int close_plugin(void* plugin_handle) {
return dlclose(plugin_handle);
} |
플러그인이랍시고 만들어본건 저러캐 생겼다능.
int conv(char* str) {
char *ptr;
ptr = str;
while(*ptr != '\0') {
*ptr = toupper(*ptr);
ptr++;
}
return 0;
} |
전체 소스코드
빌드 방법은 그리 어렵지 않으니 Makefile을 보시라능.
끵?!
'리눅스' 카테고리의 다른 글
| Ubuntu 8.10에 Ruby/SDL 설치하기 (4) | 2010/08/21 |
|---|---|
| 플러그인 만들어보기 (4) | 2010/07/24 |
| How to install Boost Library in Ubuntu 9.04 (2) | 2009/10/22 |
| The Linux Kernel - 2.0.33 (0) | 2009/08/13 |
| 내 vimrc (0) | 2009/05/19 |
| ubuntu 설치 (7) | 2008/08/17 |

linux_plugin_example.zip
댓글을 달아 주세요
으, 음난한 리눅스 프로그래밍이근염
으...음란하다니...불건전하다!
dlopen -> dlsym -> dlclose
LoadLibrary -> GetProcAddress -> FreeLibrary
이렇게 대응되는 거냐능
윈도우는 잘 모르지만...아마 그럴듯