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

平台版本: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 .py --embed      # => 生成.c

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

msg="Hello, World!"

print msg    #    =>   报错

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

print (msg)   #    =>   OK

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

gcc `python-config --cflags` `python-config --ldflags` .c -o

---------------------------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/wenbo/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通过cython编译成可执行文件方式)