周海汉 /文
2010.1.24
1.安装
zhouhh@zhouhh-home:~$ cython
程序“cython”尚未安装。 您可以使用以下命令安装:
sudo apt-get install cython
cython: command not found
zhouhh@zhouhh-home:~$ sudo apt-get install cython
...
2.写测试代码:
zhouhh@zhouhh-home:~$ vi test.pyx
def sayhello(char* str): if str == None: print 'hello world' else: print str
3.编译成C语言
zhouhh@zhouhh-home:~$ cython test.pyx
zhouhh@zhouhh-home:~$ ls test.c
test.c
test.c是由cython通过 test.pyx生成的。
4. gcc编译成.o文件
直接执行gcc会出错,必须包含python目录。
zhouhh@zhouhh-home:~$ gcc test.c
test.c:4:20: error: Python.h: 没有该文件或目录
test.c:5:26: error: structmember.h: 没有该文件或目录
test.c:7:6: error: #error Python headers needed to compile C extensions, please install development version of Python.
...
zhouhh@zhouhh-home:~$ gcc -c -fPIC -I/usr/include/python2.6 test.c
-fPIC表示编译成共享库,-I后跟include的路径。
5.生成共享库
zhouhh@zhouhh-home:~$ gcc -shared test.o -o test.so
6.在python中引用共享库
zhouhh@zhouhh-home:~$ vi test.py
import test
test.sayhello('ni hao')
7.执行
zhouhh@zhouhh-home:~$ python test.py
ni hao
8.疑问:
不知怎么穿NULL指针给函数参数。
9.参考:
http://www.cython.org/
http://blog.youkuaiyun.com/lanphaday/archive/2009/09/17/4561611.aspx
本文介绍了如何使用 Cython 安装、编写测试代码、编译为 C 语言、链接到 Python 库并最终在 Python 中调用。包括穿 NULL 指针给函数参数的方法,提供了从 Cython 到 C 扩展库构建的完整流程。
900

被折叠的 条评论
为什么被折叠?



