pythonからcの関数を呼ぶ(python + swig + ming)
sebsauvage.net : Writing C/C++ Python extensions without Microsoft Visual C++
python は Python 2.3.4 日本語環境用インストーラ(Win32)を使用
1.python23.defを作成する
python23.dllはc:\windous\system32\にある
pexports python23.dll > python23.def
2.libpython23.aを作成する
dlltool --dllname python23.dll --def python23.def --output-lib libpython23.a
これをPython23\Libsにコピーする
3.swingをダウンロードする
Download SWIGからswigwin-1.3.27をダウンロード後
swing.exeにパスを通す
4.ソースを書く
- エクスポートする関数(example.c)
#include <stdio.h> void test() { printf("aaa\n");} int initexample(){} // これがないとエラーになる
- swigの定義ファイル?(example.i)
%module example %inline %{ extern void test(); %}
- セットアップスクリプト(setup.py)
import distutils from distutils.core import setup, Extension setup(name = "Simple example from the SWIG website", version = "2.3", ext_modules = [Extension("_example", ["example.i","example.c"])])
5.ビルドする
python setup.py build -cmingw32
6.出来上がったファイルをコピー
カレントにexample.pyが、
build\lib.win32-2.3に_example.pydが出来るので、
これを、Python23\Lib\site-packagesにコピーする
7.テストしてみる
- テストコード
import example example.test()