PYTHON

swig (Simplified Wrapper and Interface Generator )

고요한하늘... 2006. 5. 10. 01:11

 

구글 개발자들이 주로 c와 java, python을 사용한다고 한다.

 

그런데 python이 편한 인터페이스의 스크립트언어이지만 속도에서는 만족할 만한 성능을 보이지 않는 부분도 있다.  이런 문제를 해결하기 위해 구글 개발자들이 주로 사용하는것이  swig 이다.

 

 

개념은 빠른 처리 속도를 요하는  함수를 c로 작성하여 so로 만들어 python에서 사용하는것이다.

 

*****************  test.c *****************

 

/* File : example.c */

int add(int a,int b)
{
   return a+b;
}

*****************  test.c *****************

 

*****************  test.i *****************

/* test.i */
%module test
%{
    /* Put header files here or function declarations like below */
%}

    extern int  add(int a,int   b);

 

*****************  test.i *****************


 

 

[linux] swig -python test.i


실행을 하고 나면 test.py와 test_wrap.c가 생성된다.


 

[linux] gcc -c test.c test_wrap.c -I/usr/include/python2.3

 

[linux] ld -shared test.o test_wrap.o -o _test.so

 

[linux] /usr/bin/python2.3
Python 2.3.4 (#1, Feb  2 2005, 11:44:49)
[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import _test
>>> _test.add(100,2)
102

 

 

python이 두,세가지 버전이 설치되어 있는 경우 컴파일 할때와 실행할때 위치가 같아야 한다.

다시 말하면 컴파일할때 /usr/include 이었으면 /usr/bin에 있는 실행파일로 실해을 해야한다.

그리고 so파일은 _(underbar)를 붙여 다른 모듈과의 충돌을 막는것이 좋다.

'PYTHON' 카테고리의 다른 글

anydbm  (0) 2006.08.25
main  (0) 2006.08.18
참고할 만한 python 사이트  (0) 2006.08.05
외부 프로그램 실행  (0) 2006.05.19
python으로 cgi만들기  (0) 2006.05.11