컴파일되는 파이썬 cython

Posted at 2010/01/03 13:24// Posted in python/pyutils
파이썬을 쓰다보면 c/c++ 의 정적 타이핑이 그리워질때가 있습니다.
리버스 엔지니어링이 쉽다는 것도 찜찜하기도 하죠.
프로그램속도에 영향을 주는 코어 부분은 좀더 빨랐으면 하는 바램이 들때가 있습니다.

cython(http://www.cython.org/)은 바로 이런 바램들을 들어주는 도구입니다.
확장된 python 문법을 사용해서 c 언어로 생성해 컴파일해서 사용할 수 있게
만들어주는 멋진 도구입니다.

distutils 를 활용하면 바로 파이썬 모듈로 뽑아낼수도 있죠 : )

모듈 스크립트 main.py

def main():
print "test -_-)/"


빌드 스크립트 _build_main.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("main", ["main.py"])]

setup(
name = 'test',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)


위처럼 작성하고 커맨드 라인 명령을 내리면
> python _build_main.py build_ext --inplace
main.pyd 가 생성됩니다.
(빌드시 문제가 생긴다면 여기 참고하세요~ )

테스트 스크립트 test.py

from main import main
main()


C:\WINDOWS\system32\cmd.exe /c test.py
test -_-)/
Hit any key to close this window...

python 의 c/c++ 언어 연동하는 대표적인 방법에는
ctypes, swig, cython 등의 3가지 방법이 있는데...

ctypes 는 간단히 dll 을 랩핑한 도구를 만들때 사용하면 좋습니다.

swig  는 c/c++ 헤더를 사용해 랩핑 코드를 만들기 때문에 헤더만 잘 만들면 외부 모듈 사용하기가 매우 쉽습니다. 생성되는 파이썬 코드를 체크하면서 튜닝을 할 수 있다는 장점이 있습니다.

cython 은 swig 영역과 조금 겹치는 것 같기도 한데... swig 를 대체한다기 보다는 파이썬 모듈의 속도향상과 리버스 엔지니어링 보호에 초점을 맞춰서 릴리즈용 빌드 스크립트로 사용하는게 좋겠다는 생각이 드는군요~

예제 다운로드~



이올린에 북마크하기(0) 이올린에 추천하기(0)
2010/01/03 13:24 2010/01/03 13:24

DevIL 윈도우에서 빌드하기

Posted at 2009/05/18 11:27// Posted in msvc
게임에서 잡다구리 이미지 포멧을 읽을때 DevIL 을 사용하고 있습니다.
몇년간 대규모 업데이트가 없어서 잊고 살다가...
쓰레드 모델 문제 때문에 소스를 다시 받아보려고 찾아가보니~
http://openil.sourceforge.net/

엄청 활발하게 활동하고 있는 것 같내요 -ㅁ-)/
윈도우 사용자를 위한 LibCompiled.zip 도 다시 지원하고 말이죠 햐햐 -ㅂ-

하지만 다운로드 소스는 윈도우 프로젝트 파일이 빠져있더군요 -_-;
요즘은 다들 SVN 저장소로 직접 다운로드 받기 때문인지 잘 관리가 안되는걸까요..

그래서 저도 SVN 저장소로 다운 받았습니다.
https://openil.svn.sourceforge.net/svnroot/openil/trunk/DevIL

윈도우는 의존성을 알아서 찾아야 하는데-_-;
매우 귀찮은 작업이므로 그냥 컴파일된 것도 받아서
http://downloads.sourceforge.net/openil/LibCompiled-vc8.zip

압축을 푼다음 src-IL/include 와 src-IL/lib 폴더에 복사해둡니다.

vc2005 솔류션은 projects/msvc8/ImageLib.sln 입니다.
src-IL 프로젝트 라이브러리 설정에 src-IL/lib 연결이 없으므로 연결해주시고요~

컴파일전에 include/config.h.win 을 include/config.h 로 이름을 변경하고 설정작업을
해줘야 합니다. 그냥 빌드하면 각종 라이브러리가 없다고 에러를 냅니다-_-


#define IL_NO_BLP
// #define IL_NO_BMP
#define IL_NO_CUT
#define IL_NO_CHEAD
#define IL_NO_DCX
// #define IL_NO_DDS
#define IL_NO_DICOM
#define IL_NO_DOOM
#define IL_NO_EXR
#define IL_NO_FITS
#define IL_NO_FTX
// #define IL_NO_GIF
// #define IL_NO_HDR
// #define IL_NO_ICO
#define IL_NO_ICNS
#define IL_NO_IWI
#define IL_NO_JP2
// #define IL_NO_JPG
#define IL_NO_LCMS
#define IL_NO_LIF
#define IL_NO_MDL
#define IL_NO_MNG
#define IL_NO_PCD
// #define IL_NO_PCX
#define IL_NO_PIC
#define IL_NO_PIX
// #define IL_NO_PNG
#define IL_NO_PNM
// #define IL_NO_PSD
// #define IL_NO_PSP
#define IL_NO_PXR
// #define IL_NO_RAW
#define IL_NO_ROT
#define IL_NO_SGI
#define IL_NO_SUN
// #define IL_NO_TGA
// #define IL_NO_TIF
#define IL_NO_TPL
#define IL_NO_WAL
#define IL_NO_WDP
#define IL_NO_XPM

// 중간 생략

//#undef ILUT_USE_DIRECTX8
#define ILUT_USE_DIRECTX9
//#define ILUT_USE_DIRECTX10
#define ILUT_USE_OPENGL
//#define ILUT_USE_SDL
#define ILUT_USE_WIN32


그냥 유명한 확장자 빼고는 전부 IL_NO_* 주석을 풀어주세요~
ILUT 플랫폼도 사용하고자하는 플랫폼만 남기고 주석처리합니다

컴파일하다보면 ilu_error.c 에서 다국어 에러 메시지 처리 때문에 에러를 내는데

#include "ilu_internal.h"
+#include "ilu_error/ilu_err-english.h"
+/*
#include "ilu_error/ilu_err-arabic.h"
#include "ilu_error/ilu_err-dutch.h"
-#include "ilu_error/ilu_err-english.h"
#include "ilu_error/ilu_err-french.h"
#include "ilu_error/ilu_err-german.h"
#include "ilu_error/ilu_err-italian.h"
#include "ilu_error/ilu_err-japanese.h"
#include "ilu_error/ilu_err-spanish.h"
+*/

-
ILconst_string *iluErrors;
ILconst_string *iluLibErrors;
ILconst_string *iluMiscErrors;
-#define ILU_NUM_LANGUAGES 8
+#define ILU_NUM_LANGUAGES 1

ILconst_string *iluErrorStrings[ILU_NUM_LANGUAGES] = {
iluErrorStringsEnglish,
+ /*
iluErrorStringsArabic,
iluErrorStringsDutch,
iluErrorStringsFrench,
@@ -36,10 +38,12 @@
iluErrorStringsSpanish,
iluErrorStringsGerman,
iluErrorStringsItalian
+ */
};

ILconst_string *iluLibErrorStrings[ILU_NUM_LANGUAGES] = {
iluLibErrorStringsEnglish,
+ /*
iluLibErrorStringsArabic,
iluLibErrorStringsDutch,
iluLibErrorStringsFrench,
@@ -47,10 +51,12 @@
iluLibErrorStringsSpanish,
iluLibErrorStringsGerman,
iluLibErrorStringsItalian
+ */
};

ILconst_string *iluMiscErrorStrings[ILU_NUM_LANGUAGES] = {
iluMiscErrorStringsEnglish,
+ /*
iluMiscErrorStringsArabic,
iluMiscErrorStringsDutch,
iluMiscErrorStringsFrench,
@@ -58,6 +64,7 @@
iluMiscErrorStringsSpanish,
iluMiscErrorStringsGerman,
iluMiscErrorStringsItalian
+ */

한글도 없고 귀찮으니 영어만 남기고 제거합니다.

~(-_-)~ 이제 사용하기만 하면 됩니다~ 짜잔~
이올린에 북마크하기(0) 이올린에 추천하기(0)
2009/05/18 11:27 2009/05/18 11:27