免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5764 | 回复: 2
打印 上一主题 下一主题

[笔记]Windows下尝试编译Pyrex扩展 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-05 16:07 |只看该作者 |倒序浏览
原文参见:http://hi.baidu.com/smallfish_xy ... efdb1f6d22eb96.html

在偶的ubuntu里编写pyrex程序编译成so还是挺爽的,用 timeit.Timer 测试性能提升不少,今天在windows也尝试了一番。

需要的工具有:
- Pyrex http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
- Dev C++ http://www.bloodshed.net/devcpp.html

Pyrex 可以通过easy_install Pyrex来安装。
Dev C++ 安装完在系统环境变量Path里加上Dev C++安装目录/bin 目录。

测试扩展代码:

# file: foo.pyx
""" simple pyrex module """
cdef class Foo:
    """ foo doc ... """
    cdef char *name
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return "foo names: %s" % (self.name)

# file: setup.py
from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext

setup(
    name='foo', ext_modules=[Extension("foo", ["foo.pyx"])],
    cmdclass={'build_ext':build_ext}
)

写好两个文件后,进入命令提示符:

C:\>python setup.py build_ext --inplace -c mingw32
running build_ext
pyrexc foo.pyx --> foo.c
building 'foo' extension
creating build
creating build\temp.win32-2.5
creating build\temp.win32-2.5\Release
C:\Program Files\DEV-CPP\Bin\gcc.exe -mno-cygwin -mdll -O -Wall -ID:\Python25\include -ID:\Python25\PC -c foo.c -o build\temp.win32-2.5\Release\foo.o writing build\temp.win32-2.5\Release\foo.def C:\Program Files\DEV-CPP\Bin\dllwrap.exe -mno-cygwin -mdll -static --entry _DllMain@12 --output-lib build\temp.win32-2.5\Release\libfoo.a --def build\temp.win32-2.5\Release\foo.def -s build\temp.win32-2.5\Release\foo.o -LD:\Python25\libs -L D:\Python25\PCBuild -lpython25 -lmsvcr71 -o foo.pyd

编译完毕,可以看到当前目录下多了:build目录、foo.c、foo.pyd。foo.pyd 即是编译好的二进制扩展。

C:\>python
ActivePython 2.5.0.0 (ActiveState Software Inc.) based on
Python 2.5 (r25:51908, Mar 9 2007, 17:40:28) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
>>> foo
<module 'foo' from 'foo.pyd'>
>>> foo.Foo("smallfish")
foo names: smallfish

论坛徽章:
0
2 [报告]
发表于 2009-08-06 09:19 |只看该作者
谢谢分享,这么好的东西居然没人顶

论坛徽章:
0
3 [报告]
发表于 2009-08-06 09:59 |只看该作者
学习一下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP