python3 c/c++ 拓展 step.py打包

使用 setup.py 来打包 C/C++ 模块为 Python Package

新建一个项目目录,结构如下:

py-cmodule-demo       # 项目目录
├── demo              # 常规的 python module,可以被import 的模块
│   ├── start.py      # 可以写入函数
│   ├── __init__.py   # 导入需要在该模块下被调用的函数
├── mymath            # c 模块目录,可以被import 的模块
│   └── mymath.c      # c 模块代码
└── setup.py          # 安装脚本

创建mymath.c

/* mymath.c */
#include <Python.h>

PyObject *sum(PyObject *self, PyObject *args) {
    int num1, num2;
    PyArg_ParseTuple(args, "ii", &num1, &num2);
    return Py_BuildValue("i", num1 + num2);
}

static PyMethodDef MymathMethods[] = { //a.python中需要拓展的函数
        {"sum", sum, METH_VARARGS, "add up two numbers and return their sum"},
        {NULL}
};

static struct PyModuleDef mymath_module = { //b.python中需要拓展的模块
        PyModuleDef_HEAD_INIT,
        "mymath", //c.模块名称
        PyDoc_STR("A hello world example to demonstrate writing a python module in c language"),
        -1,
        MymathMethods //a.对应函数名称
};

PyMODINIT_FUNC PyInit_mymath(void) { //c.PyInit_模块名称 
    PyObject *m = PyModule_Create(&mymath_module); //b.对应模块对象的函数名
    return m;
}

创建start.py

def hello():
    print("module is running")

创建__init__.py

from .demo import start
import mymath

创建setup.py

from setuptools import setup
from setuptools import Extension

mymath = Extension("mymath",
                   sources=["mymath/mymath.c"],
                   include_dirs=["/usr/include/python3"], # 编译时需要的 include dirs
                   library_dirs=["/usr/lib/x86_64-linux-gnu/"] # 编译链接时寻找链接库的目录
                   )

setup(name="demo", # 我们的 package 的名字
      version="1.0", # 版本
      description="This is a demo package",
      packages=["demo"], # 打包时需要带上的本地 python 包
      ext_modules=[mymath] # 打包时需要编译的 c 模块
      )

setup.py 安装方式

使用setup.py install安装

 sudo python3 setup.py install --record files.txt #安装
 cat files.txt |sudo xargs rm -rf #卸载

打包成whl

在dist下生成.whl文件

sudo python3 setup.py bdist_wheel

安装whl文件

pip3 install demo-1.0-cp36-cp36m-linux_aarch64.whl

卸载就像卸载pip包一样

pip3 uninstall demo

使用方式

加载整个python标准包

一个是python函数 ,一个是c拓展函数,通过__init__文件全部加载到demo包下面。
在这里插入图片描述

加载c拓展模块

在这里插入图片描述

不安装包的情况下,直接加载编译出来的so文件

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
make /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xproto.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/bigreq.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xc_misc.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/composite.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/damage.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/dpms.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/dri2.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/glx.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/randr.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/record.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/render.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/res.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/screensaver.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/shape.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/shm.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/sync.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xevie.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xf86dri.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xfixes.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xinerama.xml /usr/bin/python ./c_client.py -p //usr/lib/python3.8/site-packages //usr/share/xcb/xinput.xml Traceback (most recent call last): File "./c_client.py", line 1039, in <module> module.register() File "/usr/lib/python2.7/dist-packages/xcbgen/state.py", line 93, in register matcher.execute(self, self.namespace) File "/usr/lib/python2.7/dist-packages/xcbgen/matcher.py", line 115, in execute funcs[elt.tag](elt, module, namespace) KeyError: 'eventstruct' make: *** [Makefile:1018: xinput.c] Error 1
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图像处理大大大大大牛啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值