1 2
| export PYTHONHOME=/usr/bin/python export PYTHONPATH=/usr/bin/:/usr/lib/python27.zip:/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/lib/python2.7/site-packages
|
其中 PYTHONPATH 的内容可由以下py代码得出
1 2
| import sys print sys.path
|
一、快速转so
setup.py
1 2 3 4 5 6 7
| from distutils.core import setup from Cython.Build import cythonize
# 将需要转换的py文件放入module_list module_list = ['main.py'] setup(ext_modules=cythonize(module_list, language_level=2), script_args=['build_ext', '--inplace'])
|
二、py带main转可执行文件
2.1、将py转c
- py文件需要带”# cython : language_level=2”或”# cython : language_level=3”
1 2 3
| cython --embed main.py //等效 cython --embed -o main.c main.py
|
2.2、将c转可执行文件
2.2.1、x86-64平台
1
| x86_64-linux-gnu-gcc -O3 main.c -I /usr/include/python3.6m/ -lpython3.6m -o main
|
2.2.2、arm交叉编译平台
1
| arm-linux-gnueabihf-gcc -O3 main.c -I /opt/Crosscompile/buildComplete/python-2.7.16/include/python2.7/ -L /opt/Crosscompile/buildComplete/python-2.7.16/lib -lpython2.7 -o main-arm
|
三、py转so
3.1、将py转c
- py文件需要带”# cython : language_level=2”或”# cython : language_level=3”
1 2 3
| cython func.py //等效 cython -o func.c func.py
|
3.2、将c转o
3.2.1、x86-64平台
1 2 3 4 5 6 7 8 9 10 11 12 13
| x86_64-linux-gnu-gcc \ -pthread \ -DNDEBUG -g -fwrapv -O2 -Wall \ -fno-strict-aliasing \ -Wdate-time \ -D_FORTIFY_SOURCE=2 \ -g \ -fstack-protector-strong \ -Wformat -Werror=format-security \ -fPIC \ -I/usr/include/python3.6m \ -c func.c \ -o func.o
|
3.2.2、arm交叉编译平台
1 2 3 4 5 6 7 8 9 10 11
| arm-linux-gnueabihf-gcc \ -pthread \ -DNDEBUG -g -fwrapv -O2 -Wall \ -fno-strict-aliasing \ -D_FORTIFY_SOURCE=2 \ -g \ -Wformat -Werror=format-security \ -fPIC \ -I/opt/Crosscompile/buildComplete/python-2.7.16/include/python2.7 \ -c func.c \ -o func.o
|
3.3、将o转so
3.3.1、x86-64平台
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| x86_64-linux-gnu-gcc \ -pthread \ -DNDEBUG -g -fwrapv -O2 -Wall \ -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro \ -Wstrict-prototypes \ -fno-strict-aliasing \ -Wdate-time \ -D_FORTIFY_SOURCE=2 \ -g \ -fstack-protector-strong \ -Wformat -Werror=format-security \ -fPIC \ func.o \ -o func.so
|
3.3.2、arm交叉编译平台
1 2 3 4 5 6 7 8 9 10 11 12
| arm-linux-gnueabihf-gcc \ -pthread \ -DNDEBUG -g -fwrapv -O2 -Wall \ -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro \ -Wstrict-prototypes \ -fno-strict-aliasing \ -D_FORTIFY_SOURCE=2 \ -g \ -Wformat -Werror=format-security \ -fPIC \ func.o \ -o func.so
|