---
title: "Python 中使用 pyrex 生成 Linux 可执行文件"
author: "cloudhe"
date: "2008-08-15T14:30:27Z"
tags: []
slug: "python-pyrex-linux"
draft: false
lastmod: "2008-08-15T14:30:27Z"
id: 228379
url: "https://cloudhe.iteye.com/blog/228379"
---

这个问题是相当有意义的，如果有了比较好的方法，Python 完全可以用来开发商业软件，而不用担心源代码泄露。  
    前两天我在网上看了看，有很多的人在问这个问题。大部分的人都在用 py2exe，这是个对 Python 程序打包的东东，实际上只是在 Python 程序中找 出依赖的模块，然后发布这个程序。好处在于可以让这个程序脱离 Python 的环境来执行，不用安装 Python 解释器。但实际上并不能保护源代码，因为源 代码其实也包含在其中了。更好的办法是使用 Pyrex。在 http: //www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ 可以下载到。本来 Pyrex 是一个用来混合 编写 Python 和 C 程序的东西，然后转换为 C 代码，再编译成可执行文件。于是这里就相当于提供了一种方法可以将 Python 代码转换为 C 代码，这样就可 以把 Python 程序编译为真正的可执行文件了！下面说说怎么做。假设我们有一个简单的 Python 程序，代码如下，假设文件名为 go.py。  
 

![](../../../upimg/allimg/070704/2233250.gif)def add(a, b):  
![](../../../upimg/allimg/070704/2233250.gif)    print a+b # 为了方便看到函数调用，输出一下  
![](../../../upimg/allimg/070704/2233250.gif)    retur a+b

仅仅做一个简单的加法操作。那我们想把它编译为可执行文件的话，需要进行以下几步：  
**1.修改 go.py 文件，在里面添加一个 c 函数，当然要用 Pyrex 的语法，用这个函数来调用 Python 程序的入口函数（在这里就是 add 了）。**修改 go.py 为 go.pyx。  
在这里就是添加一个：

![](../../../upimg/allimg/070704/2233250.gif)cdef public int c\_add(int a, int b):  
![](../../../upimg/allimg/070704/2233250.gif)    return add(a, b)

**2.添加一个头文件 go.pxi, 让主函数能找到 go.py 里的 C 函数的申明**

**![](../../../upimg/allimg/070704/2233250.gif)cdef extern int (c\_add(int, int))**

**3.编写一个主函数文件 main.c，在该主函数中包含 go.h (这个文件会自动生成), 并调用前面设计的 C 函数。**

**![](../../../upimg/allimg/070704/2233250.gif)#include &lt;stdio.h&gt;**  
**![](../../../upimg/allimg/070704/2233250.gif)#include &lt;string.h&gt;**  
**![](../../../upimg/allimg/070704/2233250.gif)#include &lt;Python.h&gt;**  
**![](../../../upimg/allimg/070704/2233250.gif)#include "go.h"**  
**![](../../../upimg/allimg/070704/2233250.gif)**  
**![](../../../upimg/allimg/070704/2233250.gif)int main(int argc, char \*argv\[])**  
**![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{**  
**![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    Py\_Initialize();**  
**![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)**  
**![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    initgo(); // 这个地方的函数名字为init加上模块名，就是原来那个py文件的名字**  
**![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    c\_add(2, 5);**  
**![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)**  
**![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    Py\_Finalize();**  
**![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return 0;**  
**![](../../../upimg/allimg/070704/22332521.gif)}**

好了，现在编译就是：

pyrexc go.pyx gcc go.c main.c -I/usr/include/python2.5 -L/usr/lib/ -lpython2.5 -o go

```
 就生成了叫 go 的可执行文件，执行它，就得到了想要的结果！

注：此方法我只在 linux 上试过。（That's enough. - cloud）
 
  
From http://www.pythonid.com/html/fenleiwenzhang/lang/20070704/12.html
```