!!!Python + SWIG *投稿者: みゅ *カテゴリ: Python *優先度: 普通 *状態: 完了 *日時: 2010年01月01日 17時39分17秒 //{{bugstate}} !!内容 *メモ.だけど以下のページのまったくの書き写しです.ごめんなさい. !![Python/Swig|http://silkychild.ddo.jp/pwiki/index.php?Python%2FSwig] *管理人様、ありがとうございます. *そのうち、独自の情報も追加予定・・・ *C のソース /* hello.c */ int print_hello() { printf("hello\n"); return 0; } *hello.i /* hello.i */ %module hello %{ /* Put header files here (optional) */ extern int print_hello(); %} extern int print_hello(); *コマンド その1 $ swig -python hello.i *hello.pyとhello_wrap.cができる **hello_wrap.cがラッパーなんでしょうね? *hello.pyはこんな感じ $ vi hello.py # This file was automatically generated by SWIG (http://www.swig.org). # Version 1.3.36 # # Don't modify this file, modify the SWIG interface instead. # This file is compatible with both classic and new-style classes. import _hello import new new_instancemethod = new.instancemethod try: _swig_property = property except NameError: pass # Python < 2.2 doesn't have 'property'. def _swig_setattr_nondynamic(self,class_type,name,value,static=1): ・・・(続く) *コマンド その2 $ gcc -c hello.c hello_wrap.c -I /usr/include/python2.5 hello.c: In function ‘print_hello’: hello.c:3: 警告: incompatible implicit declaration of built-in function ‘printf’ $ gcc -shared hello.o hello_wrap.o -o _hello.so /usr/lib/gcc/i486-pc-linux-gnu/4.3.2/../../../../i486-pc-linux-gnu/bin/ld: warni ng: creating a DT_TEXTREL in object. *以下のようになる $ ls -la 合計 184 drwxr-xr-x 2 admin admin 4096 2010-01-01 17:30 . drwxr-xr-x 8 admin admin 4096 2010-01-01 17:29 .. -rwxr-xr-x 1 admin admin 34916 2010-01-01 17:30 _hello.so -rw-r--r-- 1 admin admin 56 2010-01-01 17:28 hello.c -rw-r--r-- 1 admin admin 126 2010-01-01 17:28 hello.i -rw-r--r-- 1 admin admin 864 2010-01-01 17:30 hello.o -rw-r--r-- 1 admin admin 1609 2010-01-01 17:29 hello.py -rw-r--r-- 1 admin admin 91083 2010-01-01 17:29 hello_wrap.c -rw-r--r-- 1 admin admin 27872 2010-01-01 17:30 hello_wrap.o *コマンド その3 $ python Python 2.5.4 (r254:67916, Jun 8 2009, 13:35:50) [GCC 4.1.2 (Gentoo 4.1.2 p1.3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import hello >>> hello.print_hello() hello 0 >>> !![SWIGでPythonラッパを書いてみる|http://d.hatena.ne.jp/sleepy_yoshi/20091123/p1]の例 *(管理人様ありがとうございます.) *example.c #include void print_hoge(int n) { int i; for (i = 0; i < n; i++) { printf("hoge\n"); } } void print_hoge2(char *c){ printf("%s\n", c); } *example.i %module example %{ /* Write include header (Optional) */ /* #include "example.h" */ /*extern void print_hoge (int n);*/ %} extern void print_hoge (int n); extern void print_hoge2(char *c); *コマンド1 swig -python example.i *コマンド2 gcc -c example.c example_wrap.c -I /usr/include/python2.5 *コマンド3 gcc -shared example.o example_wrap.o -o _example.so *結果 python >>> import example >>> example.print_hoge(5) hoge hoge hoge hoge hoge >>> example.print_hoge2('test') test !!作成したモジュールを使うために *PYTHONPATHを設定する PYTHONPATH='/home/admin/swig_test' python *[Python入門(35) - インポートの処理|http://atkonn.blogspot.com/2008/02/python-python34_15.html] *[Python でライブラリへのパスを追加する方法|http://jamz.jp/weblog/2009/01/python-module-path.html] **↑から引用 => あるライブラリの最新のリビジョンに追随していきた場合もあると思います。その場合は、Subversionやsvkで通常通りチェックアウトを行い、site-package内に、pthファイルを作って、インポートするディレクトリを指定します。 *<取り消し線>ということは、mod_pythonで使えるようにするには、apacheの起動スクリプトにPYTHONPATHを設定するか、PYTHONPATH変数を指定して、apacheを起動するとよいのかも.</取り消し線> !!リンク *[SWIG連携による実行速度効率化|http://python.matrix.jp/tips/cpp_extension.html] *[pythonとc++のインターフェイスの比較 swig boost.python ctypes|http://d.hatena.ne.jp/niitsuma/20080209/1203184397] *[SWIGでPythonラッパを書いてみる|http://d.hatena.ne.jp/sleepy_yoshi/20091123/p1] *[Python/Swig|http://silkychild.ddo.jp/pwiki/index.php?Python%2FSwig] *[SWIGのインストール|http://www.02.246.ne.jp/~torutk/cxx/swig/install.html] **Gentooはportageにあったので、らくちんでした *[19 SWIG and Python|http://blog.ssktkr.com/swig-jp/_/Python.html] **[本家|http://www.swig.org/Doc1.3/Python.html] *http://tasuku.suenaga.name/pub/pytc/Python-unconference01-cextension.pdf !!コメント //{{comment}}