如何使用Cython将Python 3编译为C [英] How to use Cython to compile Python 3 into C

查看:319
本文介绍了如何使用Cython将Python 3编译为C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Python 3脚本转换为C,然后将该C文件编译为可执行文件。

I'm trying to convert a Python 3 script into C and then compile that C file into an executable.

我有一个简单的python脚本:

I have this simple python script:

def greet(name = ""):
  print("Hello {0}".format(name if len(name) > 0 else "World"))

greet("Mango")

我已使用以下脚本将此脚本转换为C:

I've converted this script into C using:

cython greet.py -o greet.c

然后我使用以下命令编译了C文件:

Then I've compiled the C file using:

cc greet.c -o greet

之后我输入的最后一条命令我得到了错误:

After I entered the last command I got the error:


严重错误:Python.h:没有这样的文件或目录编译终止。

fatal error: Python.h: No such file or directory compilation terminated.

收到错误后,我回过头来意识到我正在使用Python3,并且忘记了 cython之后的 3。 br />
因此,使用以下命令重新编译了python脚本:

After I got the error I went back and realised that I was using Python3 and that I had forgot the "3" after "cython".
So re-compiled the python script using:

cython3 greet.py -o greet.c

然后尝试使用以下方法重新编译C文件:

Then attempted to re-compile the C file using:

cc greet.c -o greet

再次失败,并抛出相同的错误,所以我继续在SO和Google上搜索,发现了以下问题:

Again this failed and threw the same error so I went searching on SO and Google and found these questions:

  • fatal error: Python.h: No such file or directory
  • I have Python on my Ubuntu system, but gcc can't find Python.h
  • https://askubuntu.com/questions/526708/fatal-error-python-h-no-file-or-directory

这些问题中的所有答案均无效。

None of these answers in these questions work.

我已确保已安装cython所有正确的依存关系使用 apt-get install pip install 的用户遗憾地认为它似乎仍然无法正常工作。

I've made sure that I have installed cython all of the correct dependencies using apt-get install and pip install sadly thought it still does not seem to work.

推荐答案

检查文档。 gcc xc -ox 还不够。

此页说明了编译: http://docs.cython.org/src/reference/compilation.html

还有很多,但是直接的答案是:

There's a lot more to it, but a direct answer is:


编译.c文件会有所不同取决于您的操作系统。用于编写扩展模块的Python文档应包含有关系统的一些详细信息。这里我们以Linux系统为例:

Compiling your .c files will vary depending on your operating system. Python documentation for writing extension modules should have some details for your system. Here we give an example on a Linux system:

$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing
- I / usr / include / python2.7 -o yourmod.so yourmod.c

$ gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o yourmod.so yourmod.c

当然,在您的情况下更接近 -I / usr / include / python3.4 ,甚至 $(pkg-config --libs --cflags python3)。而且您没有使用共享的进行构建,因为您想要一个可执行文件。

Of course in your situation it's going to be something closer to -I/usr/include/python3.4, or even $(pkg-config --libs --cflags python3). And you're not building with -shared, because you want an executable.

最短的时间这必须有效命令集为:

Shortest "this has to work" set of commands is:

cython3 --embed greet.py -o greet.c
gcc $(pkg-config --libs --cflags python3) greet.c -o greet

您需要安装 pkg-config (如果丢失)。

You need to install pkg-config if it's missing.

这篇关于如何使用Cython将Python 3编译为C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆