LoginSignup
1
1

More than 5 years have passed since last update.

cython helloworld

Posted at

cythonを初めて使いました。簡単なモジュールを動かすだけでもいろいろつまずいたのでメモとして残しておきます。

環境:Mac OS El Capitan

ビルド手順

Learning Cython Programming - Second Editionを参考にしました。コードはここあります。

本では.pyxファイルを作っていますが、普通の.pyファイルを使って試しました。

cython -o helloworld.c helloworld.py

次にコンパイルをします。

gcc -g -O2 -fpic -c helloworld.c -o helloworld.o `python-config --cflags`
gcc -g -O2 -shared -o helloworld.so helloworld.o `python-config --libs`

こうして出来た.soファイルをimportすると、segmentation faultが発生しました。
この問題については github に issueが報告されており、gccに渡すオプションを変更すればエラーは起きませんでした。

gcc -g -O2 -fpic -c helloworld.c -o helloworld.o `python-config --cflags`
gcc -g -O2 -shared -o helloworld.so helloworld.o `python-config --ldflags`

また、cythonのドキュメントには、distutilsを使う手順が記載されており、こちらの方が簡単だと思いました。

Symbol not found: _PyUnicodeUCS4_Compare

練習が終わったので、自分がターゲットとしているモジュールをcythonでビルドしました。いざ使ってみると、Symbol not found: _PyUnicodeUCS4_Compare というImportErrorエラーが発生しました。

こちらの記事を参考に、virtualenvでインストールしていた python 2.7.12 を削除し、Macのシステムのpythonで動かしてみたらちゃんと使えました。

ビルド時にどのモジュールとリンクするかみたいな問題でしょうか? 素のpythonと違って、cythonは面倒ですねぇ。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1