首页 分享 python通过cython编译成可执行文件方式

python通过cython编译成可执行文件方式

来源:萌宠菠菠乐园 时间:2024-10-17 02:58

平台版本:redhat7,python2.7


1. 安装 python 开发版

sudo yum install python-devel

如果没有安装python-devel, 后面第4步编译会遇到Python.h找不到的错误:

bash: python-config: command not found...
bash: python-config: command not found...
eg.c:4:20: fatal error: Python.h: No such file or directory
 #include "Python.h"
                    ^
compilation terminated.

2. 安装cython:

sudo pip install Cython --install-option="--no-cython-compile"


3. 转*.py成 *.c:

cython <file_name>.py --embed      # => 生成<file_name>.c

这一步要注意,有一些python不会报错的语法,在转译时候会报错。语法最好写完整,比如下面语法python2不会报错,

msg="Hello, World!"

print msg    #    =>   报错

但转译会报错,改成后者就不会报错:

print (msg)   #    =>   OK

4. 编译*.C 成 exe文件:

gcc `python-config --cflags` `python-config --ldflags` <file_name>.c -o <exe_name>

---------------------------2019.09.14-------------------------------

在编译一个比较复杂的脚本时遇到以下错:

1.

"

/usr/lib/python2.7/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /workspace/xxx/script/reg_gen/gen_reg.py
  tree = Parsing.p_module(s, pxd, full_module_name)
/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

"

解决方案: 在脚本第一行加:

# cython: language_level=3

2. gcc编译时遇到:

/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

解决方案:gcc中加option “-shared”

3. 成功编译成exe文件,执行时候遇到:

Segmentation fault (core dumped)

待解决

相关知识

Python笔试题
通过python
知攻善防Web1应急靶机笔记
Python学习手册
python运行run在哪
python——is和==区别
桌面宠物 ① 通过python制作属于自己的桌面宠物
Python小练习
Python 发送邮件
python入门教程NO.1 打印你的宠物小精灵

网址: python通过cython编译成可执行文件方式 https://www.mcbbbk.com/newsview403432.html

所属分类:萌宠日常
上一篇: 支付宝PC网站支付与wap端网站
下一篇: 平价猫罐头测评来啦

推荐分享