使用cython把python编译so
2024-08-30 10:20:27
1.需求
为了保证线上代码安全和效率,使用python编写代码,pyc可直接反编译,于是把重要代码编译so文件
2.工作
2.1 安装相关库:
pip install cython
yum install -y gcc python-devel
2.2 创建setup.py
#!/usr/bin/env python
# coding=utf-8
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("xxxx.py")
)
2.3 运行
root@868aec72d1a4:/xxx/xxx/xxx# python setup.py build_ext
Compiling xxx.py because it changed.
[1/1] Cythonizing xxx.py
running build_ext
building 'lib.xxx' extension
creating build
creating build/temp.linux-x86_64-2.7
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c xxx.c -o build/temp.linux-x86_64-2.7/xxx.o
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/lib
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/xxx.o -o build/lib.linux-x86_64-2.7/lib/xxx.so
会在当年目录上传build目录,结构如下:
root@868aec72d1a4:/spider/codes/lib# tree build/
build/
|-- lib.linux-x86_64-2.7
| `-- lib
| `-- xxx.so
`-- temp.linux-x86_64-2.7
`-- xxx.o 3 directories, 2 files
2.4 获取so文件即可
最新文章
- js中 javascript:void(0) 用法详解
- AOP 手动,半自动,全自动
- backup3
- ReactJS学习笔记(三)
- Android简化xml sax解析
- Spring Boot+AngularJS中因为跨域导致Session丢失
- Oracle用户、权限、角色管理
- .NET MVC4 实训记录之七(实现资源的自主访问后续)
- zf-关于把某个地址的svn项目移动到另一个上面的步骤
- spring注解一次 清除多个缓存
- 魔术常量(Magic constants)
- Esptouch移植xamarin记要
- Java作业一 (2017-9-10)
- 浏览器对象模型BOM总结
- Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模
- urllib模块学习
- linux命令学习之:systemctl
- [Umbraco] 自定义DataType中Data Editor Setting Type
- Python 3.6 安装pip
- php判断是否为时间戳