python3调用c

c函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <Python.h>

int great_function(int a) {
return a + 1;
}

static PyObject * _great_function(PyObject *self, PyObject *args) {
int _a;
int res;
if (!PyArg_ParseTuple(args, "i", &_a))
return NULL;
res = great_function(_a);
return PyLong_FromLong(res);
}

static PyMethodDef GreateModuleMethods[] = {
{
"great_function",
_great_function,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef great_module = {
PyModuleDef_HEAD_INIT,
"great_module",
NULL,
-1,
GreateModuleMethods
};

PyMODINIT_FUNC PyInit_great_module(void)
{
PyObject *m;
m = PyModule_Create(&great_module);
if (m == NULL)
return NULL;
printf("init great_module module\n");
return m;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <Python.h>

int great_function(int a) {
return a + 1;
}

static PyObject * _great_function(PyObject *self, PyObject *args) {
int _a;
int res;
if (!PyArg_ParseTuple(args, "i", &_a))
return NULL;
res = great_function(_a);
return PyLong_FromLong(res);
}

static PyMethodDef GreateModuleMethods[] = {
{
"great_function",
_great_function,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef great_module = {
PyModuleDef_HEAD_INIT,
"great_module",
NULL,
-1,
GreateModuleMethods
};

PyMODINIT_FUNC PyInit_great_module(void)
{
PyObject *m;
m = PyModule_Create(&great_module);
if (m == NULL)
return NULL;
printf("init great_module module\n");
return m;
}

setup.py

1
2
3
4
from setuptools import setup, Extension

great_module = Extension('great_module', sources=["great_module.c"])
setup(ext_modules=[great_module])

或者是用gcc

1
g++ -fPIC -shared great_class_wrap.cxx -o _great_class.so  -I/usr/include/python2.7/ -lpython2.7

原始的c函数文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <Python.h>

int great_function(int a) {
return a + 1;
}

static PyObject * _great_function(PyObject *self, PyObject *args)
{
int _a;
int res;

if (!PyArg_ParseTuple(args, "i", &_a))
return NULL;
res = great_function(_a);
return PyLong_FromLong(res);
}

static PyMethodDef GreateModuleMethods[] = {
{
"great_function",
_great_function,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initgreat_module(void) {
(void) Py_InitModule("great_module", GreateModuleMethods);
}
1
gcc -fPIC -shared great_module.c -o great_module.so -I/home/xxx/miniconda2/envs/env3.6/include/python3.6m -lpython3.6m

多个参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <Python.h>

int great_function(int a, int b) {
return a + b;
}

static PyObject * _great_function(PyObject *self, PyObject *args) {
int _a;
int _b;
int res;
if (!PyArg_ParseTuple(args, "ii", &_a, &_b))
return NULL;
res = great_function(_a, _b);
return PyLong_FromLong(res);
}

static PyMethodDef GreateModuleMethods[] = {
{
"great_function",
_great_function,
METH_VARARGS,
""
},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef great_module = {
PyModuleDef_HEAD_INIT,
"great_module",
NULL,
-1,
GreateModuleMethods
};

PyMODINIT_FUNC PyInit_great_module(void)
{
PyObject *m;
m = PyModule_Create(&great_module);
if (m == NULL)
return NULL;
printf("init great_module module\n");
return m;
}
请作者喝一杯咖啡☕️