<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>smallfish logs &#187; Cython</title>
	<atom:link href="http://chenxiaoyu.org/blog/archives/tag/cython/feed" rel="self" type="application/rss+xml" />
	<link>http://chenxiaoyu.org</link>
	<description>关注 Python &#38; Go &#38; PostgreSQL</description>
	<lastBuildDate>Sat, 07 Aug 2010 10:31:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cython 教程 &#8211; 调用外部C语言函数</title>
		<link>http://chenxiaoyu.org/blog/archives/275</link>
		<comments>http://chenxiaoyu.org/blog/archives/275#comments</comments>
		<pubDate>Sun, 30 May 2010 11:11:01 +0000</pubDate>
		<dc:creator>smallfish</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Cython]]></category>

		<guid isPermaLink="false">http://chenxiaoyu.org/?p=275</guid>
		<description><![CDATA[一般情况完全可以在 Python 里导入 from math import sin 然后调用 sin() 函数。然而，调用C里面的 sin() 函数速度会更快，尤其在复杂的循环里。在 Cython 里可以这样声明和使用：
cdef extern from "math.h":
    double sin(double)

cdef double f(double x):
    return sin(x*x)
请注意，上面的代码声明了 math.h 里的函数，提供给 Cython 使用。C编译器在编译时将会看到 math.h 的声明，但 Cython 不会去分析 math.h 和单独的定义。
当调用一个C函数时，一定要注意引入适当的链接库。这个依赖于特定的平台；下面的例子可以在Linux和Mac OS X下运行：
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules=[
    Extension("demo",
 [...]]]></description>
			<content:encoded><![CDATA[<p>一般情况完全可以在 Python 里导入 from math import sin 然后调用 sin() 函数。然而，调用C里面的 sin() 函数速度会更快，尤其在复杂的循环里。在 Cython 里可以这样声明和使用：</p>
<pre>cdef extern from "math.h":
    double sin(double)

cdef double f(double x):
    return sin(x*x)</pre>
<p>请注意，上面的代码声明了 math.h 里的函数，提供给 Cython 使用。C编译器在编译时将会看到 math.h 的声明，但 Cython 不会去分析 math.h 和单独的定义。</p>
<p>当调用一个C函数时，一定要注意引入适当的链接库。这个依赖于特定的平台；下面的例子可以在Linux和Mac OS X下运行：</p>
<pre>from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules=[
    Extension("demo",
              ["demo.pyx"],
              libraries=["m"]) # Unix-like specific
]

setup(
  name = "Demos",
  cmdclass = {"build_ext": build_ext},
  ext_modules = ext_modules
)</pre>
<p>跟从 math 库里使用 sin() 函数一样，它可以声明和导入任何C库，Cython会生成正确的共享或者静态链接库。</p>
<p>参考：<a href="http://docs.cython.org/src/tutorial/external.html">http://docs.cython.org/src/tutorial/external.html</a></p>
<hr />
<p><small>© smallfish for <a href="http://chenxiaoyu.org">smallfish logs</a>, 2010. |
<a href="http://chenxiaoyu.org/blog/archives/275">Permalink</a> |
<a href="http://chenxiaoyu.org/blog/archives/275#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://chenxiaoyu.org/blog/archives/275&title=Cython 教程 &#8211; 调用外部C语言函数">del.icio.us</a>
<br/>
Post tags: <a href="http://chenxiaoyu.org/blog/archives/tag/cython" rel="tag">Cython</a><br/>
</small></p>
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-8914011260472945";
/* 468x60, 创建于 09-11-19 */
google_ad_slot = "7198645178";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://chenxiaoyu.org/blog/archives/275/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cython参考指南 &#8211; 编译</title>
		<link>http://chenxiaoyu.org/blog/archives/60</link>
		<comments>http://chenxiaoyu.org/blog/archives/60#comments</comments>
		<pubDate>Thu, 19 Nov 2009 07:03:13 +0000</pubDate>
		<dc:creator>smallfish</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Cython]]></category>

		<guid isPermaLink="false">http://chenxiaoyu.org/blog/?p=60</guid>
		<description><![CDATA[Cython代码跟Python不一样，必须要编译。
编译经过两个阶段：
* Cython编译.pyx文件为.c文件
* C编译器会把.c文件编译成.so文件(Windows上是.pyd)
以下会分节介绍几种方式来建立你的扩展模块。
注意： -a 选项，如果使用该选项将会为.c文件生成一份很漂亮的HTML文件，双击高亮的章节部分会展开代码，这对理解，优化和调试模块将会非常有帮助。
命令行
从命令行执行Cython编译器，输入选项和.pyx文件列表。
$ cython -a yourmod.pyx
会生成一个yourmod.c文件（指定-a选项会生成一个HTML文件）
编译.c文件取决于你的操作系统，请参考下如何在你的系统写Python扩展模块文档。
下面是一个Linux系统的例子：
$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing \
 -I/usr/include/python2.5 -o yourmod.so yourmod.c
gcc需要提供包含的文件和扩展库的链接。
在目录里会生成yourmod.so文件。
现在只需要导入你的yourmod模块就可以了。

Distutils
确保你的系统已经安装好Distutils。
下面假设需要编译的文件叫hello.pyx。
建立一个setup.py的脚本：
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("hello", ["hello.pyx"])]

setup(
    name = ’Hello world app’,
    cmdclass = {’build_ext’: build_ext},
    ext_modules = ext_modules
)
在命令行执行：python setup.py [...]]]></description>
			<content:encoded><![CDATA[<p>Cython代码跟Python不一样，必须要编译。</p>
<p>编译经过两个阶段：</p>
<p>* Cython编译.pyx文件为.c文件</p>
<p>* C编译器会把.c文件编译成.so文件(Windows上是.pyd)</p>
<p>以下会分节介绍几种方式来建立你的扩展模块。</p>
<p>注意： -a 选项，如果使用该选项将会为.c文件生成一份很漂亮的HTML文件，双击高亮的章节部分会展开代码，这对理解，优化和调试模块将会非常有帮助。</p>
<p><strong>命令行</strong></p>
<p>从命令行执行Cython编译器，输入选项和.pyx文件列表。</p>
<pre>$ cython -a yourmod.pyx</pre>
<p>会生成一个yourmod.c文件（指定-a选项会生成一个HTML文件）</p>
<p>编译.c文件取决于你的操作系统，请参考下如何在你的系统写Python扩展模块文档。</p>
<p>下面是一个Linux系统的例子：</p>
<pre>$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing \
 -I/usr/include/python2.5 -o yourmod.so yourmod.c</pre>
<p>gcc需要提供包含的文件和扩展库的链接。</p>
<p>在目录里会生成yourmod.so文件。</p>
<p>现在只需要导入你的yourmod模块就可以了。<br />
<strong><br />
Distutils</strong></p>
<p>确保你的系统已经安装好Distutils。</p>
<p>下面假设需要编译的文件叫hello.pyx。</p>
<p>建立一个setup.py的脚本：</p>
<pre>from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("hello", ["hello.pyx"])]

setup(
    name = ’Hello world app’,
    cmdclass = {’build_ext’: build_ext},
    ext_modules = ext_modules
)</pre>
<p>在命令行执行：python setup.py build_ext &#8211;inplace</p>
<p>现在可以在shell或者脚本里正常导入使用了。<br />
<strong><br />
Pyximport</strong></p>
<p>在纯Python代码里调用Cython代码：</p>
<pre>&gt;&gt;&gt; import pyximport; pyximport.install()
&gt;&gt;&gt; import helloworld
Hello World</pre>
<p>这仅仅是简单调用Cython，不需要C库也不需要构建脚本。</p>
<p>当然也可以实验性的在Python调用。允许在Python模块中运行Cython代码在任何一个.pyx和.py模块。这包<br />
括标准库和包。如果Cython编译失败的话，pyximport会返回到加载失败的模块处。</p>
<p>.py安装是这样：</p>
<pre>&gt;&gt;&gt; pyximport.install(pyimport = True)</pre>
<p>原文：<a href="http://docs.cython.org/src/reference/compilation.html" target="_blank">http://docs.cython.org/src/reference/compilation.html</a></p>
<hr />
<p><small>© smallfish for <a href="http://chenxiaoyu.org">smallfish logs</a>, 2009. |
<a href="http://chenxiaoyu.org/blog/archives/60">Permalink</a> |
<a href="http://chenxiaoyu.org/blog/archives/60#comments">5 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://chenxiaoyu.org/blog/archives/60&title=Cython参考指南 &#8211; 编译">del.icio.us</a>
<br/>
Post tags: <a href="http://chenxiaoyu.org/blog/archives/tag/cython" rel="tag">Cython</a>, <a href="http://chenxiaoyu.org/blog/archives/tag/python" rel="tag">Python</a><br/>
</small></p>
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-8914011260472945";
/* 468x60, 创建于 09-11-19 */
google_ad_slot = "7198645178";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://chenxiaoyu.org/blog/archives/60/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
