还是比较简单的,这次就权当入个门吧
写好一个C函数
1234567891011121314151617181920212223static PyObject *hellozkw(PyObject *self, PyObject *args) {int num;//解析参数if (!PyArg_ParseTuple(args, "i", &num)) {return Py_BuildValue("i", -1);}printf("hello zkw %d\n", num);return Py_BuildValue("i", NULL);}static PyMethodDef HMethods[] = {//方法名,导出函数,参数传递方式,方法描述。{"hellozkw", hellozkw, METH_VARARGS, "hahahaha.... from zkw's hello"},{NULL, NULL, 0, NULL}};void inithello(void) {(void) Py_InitModule("hello", HMethods);}准备一个setup文件
12345678#!/usr/bin/env python# coding=utf-8from distutils.core import setup, Extensionmodule = Extension('hello', sources = ['hello.c'])setup(name = 'hello test', version = '1.0', ext_modules = [module])Makefile文件
123publish:python setup.py buildpython setup.py install运行make publush
- 运行即可12345import hello123)ret = hello.hellozkw(hello zkw 123ret0