cythonコマンドを使ったcythonコードのビルド
Cythonのtutorialでsetup.pyを使うやり方は載ってるのだがcythonコマンドでやる方法は 詳述されてなかったので、メモ.
Learning Cython Programming | PACKT Booksより.Cythonコンパイルの手順
上の作業を実行する方法はいくつかあるが,tutorialで詳しくふれられていて、一般的な方法はsetup.pyを書いてpythonのdistutils パッケージを利用する方法だ。OSやマシンの違いを吸収する安全な方法としてはこれが一番いいのかもしれない。 しかし、ちょっとした確認のためにいちいちsetup.pyを用意するもの面倒だ.
そこでこれ以外にも以下で示すように直接cython
コマンドをつかって”手で”コンパイルする方法がある*1。
Step 1 : cythonコマンドで.pyx
ファイル→ .c
ファイルに変換.
helloworld.pyx
print "test"
を用意して、.c
ファイルをcythonファイルで生成する.
$ cython --embed helloworld
ここで--embed
オプションをつけておかないと後でgcc
でコンパイルするときにmain()
関数がないと怒られる
--embed[=<method_name>]
Generate a main() function that embeds the Python interpreter.
Step2 : gcc
をつかってsharedライブラリを作る
pkg-config
コマンドが使えるなら使うと便利だ.
$ gcc `pkg-config --cflags --libs python` -o helloworld.so helloworld.c"
> import helloworld #test
*1:マニュアルには"This is mostly for debugging and experimentation. "と注意書きがある