'linux'에 해당되는 글 3

  1. 2010/07/24 플러그인 만들어보기 (4)
  2. 2010/05/21 "No available video device" error with SDL (2)
  3. 2009/08/13 The Linux Kernel - 2.0.33
 

플러그인 만들어보기

리눅스 | 2010/07/24 09:59 | Posted by DMW
크리에이티브 커먼즈 라이선스
Creative Commons License
리눅스에서는 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


메인 프로그램은 아래처럼 생겼고....

#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);
}

 플러그인이랍시고 만들어본건 저러캐 생겼다능.

1
2
3
4
5
6
7
8
9
10
11
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

댓글을 달아 주세요

  1. Favicon of http://lazygyu.tistory.com BlogIcon LazyGyu 2010/08/02 10:04  댓글주소  수정/삭제  댓글쓰기

    으, 음난한 리눅스 프로그래밍이근염

  2. Favicon of http://www.filepang.co.kr BlogIcon DMW 2010/08/02 21:07  댓글주소  수정/삭제  댓글쓰기

    으...음란하다니...불건전하다!

  3. Favicon of http://jkherald.com BlogIcon 정기 2010/08/06 15:43  댓글주소  수정/삭제  댓글쓰기

    dlopen -> dlsym -> dlclose
    LoadLibrary -> GetProcAddress -> FreeLibrary
    이렇게 대응되는 거냐능

"No available video device" error with SDL

뻘글들 | 2010/05/21 03:26 | Posted by DMW
크리에이티브 커먼즈 라이선스
Creative Commons License


"No available video device" error with SDL 

이런 에러를 만나면서 SDL 어플이 실행이 안되면

running "aptitude install gnome-devel" then reconfinguring and compiling sdl

이렇게 대처한다. 까먹을까바 블로그에 백ㅋ업ㅋ
저작자 표시 비영리 동일 조건 변경 허락

댓글을 달아 주세요

  1. 2010/05/21 15:42  댓글주소  수정/삭제  댓글쓰기

    비밀댓글입니다

The Linux Kernel - 2.0.33

리눅스 | 2009/08/13 11:48 | Posted by DMW
크리에이티브 커먼즈 라이선스
Creative Commons License

 옛날 버젼인 2.0.33을 다루고 있는 문서라능. 무지 옛날 버젼이긴 하지만 당시에 2.0.33 버젼의 커널로 많은 서버들이 돌았을것을 생각해보면 나쁠꺼 업ㅂ다. 거기다 한글로 번역되이다!!! 커널 용량이 작고 최신 버젼보다 자원을 덜 소모한다는게 장점이라면 장점이라능. 번역해주신 분들께 매우 감사하다능.

난 텍스트 파일로 가지고 있긴한데...올리려면 저자의 허가를 받아야된단다. 그래서 올리진 못하겠고...저기가면 볼 수 있다.

HTML 버젼
http://wiki.kldp.org/Translations/html/The_Linux_Kernel-KLDP/The_Linux_Kernel-KLDP.html

PDF 버젼
http://linux.flyduck.com/kernel/tlk/tlk-0.1.0/tlk.pdf  
저작자 표시 비영리 동일 조건 변경 허락

'리눅스' 카테고리의 다른 글

플러그인 만들어보기  (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
-_-;;  (0) 2007/11/29

댓글을 달아 주세요