C语言调用python代码
C语言源代码
- C语言源代码,embed.c
/*
* embed.c
*
* Created on: 2022年12月30日
* Author: xzlAwin
*/
#include <Python.h>
/* Execute func(x,y) in the Python interpreter. The
arguments and return result of the function must
be Python floats */
double call_func(PyObject *func, double x, double y) {
PyObject *args;
PyObject *kwargs;
PyObject *result = 0;
double retval;
/* Make sure we own the GIL */
PyGILState_STATE state = PyGILState_Ensure();
/* Verify that func is a proper callable */
if (!PyCallable_Check(func)) {
fprintf(stderr,"call_func: expected a callable\n");
goto fail;
}
/* Build arguments */
args = Py_BuildValue("(dd)", x, y);
kwargs = NULL;
/* Call the function */
result = PyObject_Call(func, args, kwargs);
Py_DECREF(args);
Py_XDECREF(kwargs);
/* Check for Python exceptions (if any) */
if (PyErr_Occurred()) {
PyErr_Print();
goto fail;
}
/* Verify the result is a float object */
if (!PyFloat_Check(result)) {
fprintf(stderr,"call_func: callable didn't return a float\n");
goto fail;
}
/* Create the return value */
retval = PyFloat_AsDouble(result);
Py_DECREF(result);
/* Restore previous GIL state and return */
PyGILState_Release(state);
return retval;
fail:
Py_XDECREF(result);
PyGILState_Release(state);
abort();
}
/* Load a symbol from a module */
PyObject *import_name(const char *modname, const char *symbol) {
PyObject *u_name, *module;
u_name = PyUnicode_FromString(modname);
module = PyImport_Import(u_name);
Py_DECREF(u_name);
return PyObject_GetAttrString(module, symbol);
}
/* Simple embedding example */
int main() {
PyObject *pow_func;
double x;
Py_Initialize();
/* Get a reference to the math.pow function */
pow_func = import_name("math","pow");
/* Call it using our call_func() code */
for (x = 0.0; x < 10.0; x += 0.1) {
printf("%0.2f %0.2f\n", x, call_func(pow_func,x,2.0));
}
/* Done */
Py_DECREF(pow_func);
Py_Finalize();
system("pause");
return 0;
}
编译配置文件
- win10环境,Makefile配置文件
all::
gcc -W -I F:/sdk/python/Python37/include \
-L F:/sdk/python/Python37/libs embed.c -l python37
- Linux环境,Makefile配置文件
all::
cc -g embed.c -I/usr/local/include/python3.3m \
-L/usr/local/lib/python3.3/config-3.3m -lpython3.3m
运行编译命令
- win10环境
gcc -W -I F:/sdk/python/Python37/include -L F:/sdk/python/Python37/libs embed.c -l python37
- Linux环境
cc -g embed.c -I/usr/local/include/python3.3m \
-L/usr/local/lib/python3.3/config-3.3m -lpython3.3m
测试
运行成功,显示
0.00 0.00
0.10 0.01
0.20 0.04
0.30 0.09
0.40 0.16
0.50 0.25
0.60 0.36
0.70 0.49
0.80 0.64
0.90 0.81
1.00 1.00
1.10 1.21
1.20 1.44
1.30 1.69
1.40 1.96
1.50 2.25
1.60 2.56
1.70 2.89
1.80 3.24
1.90 3.61
2.00 4.00
2.10 4.41
2.20 4.84
2.30 5.29
2.40 5.76
2.50 6.25
2.60 6.76
2.70 7.29
2.80 7.84
2.90 8.41
3.00 9.00
3.10 9.61
3.20 10.24
3.30 10.89
3.40 11.56
3.50 12.25
3.60 12.96
3.70 13.69
3.80 14.44
3.90 15.21
4.00 16.00
4.10 16.81
4.20 17.64
4.30 18.49
4.40 19.36
4.50 20.25
4.60 21.16
4.70 22.09
4.80 23.04
4.90 24.01
5.00 25.00
5.10 26.01
5.20 27.04
5.30 28.09
5.40 29.16
5.50 30.25
5.60 31.36
5.70 32.49
5.80 33.64
5.90 34.81
6.00 36.00
6.10 37.21
6.20 38.44
6.30 39.69
6.40 40.96
6.50 42.25
6.60 43.56
6.70 44.89
6.80 46.24
6.90 47.61
7.00 49.00
7.10 50.41
7.20 51.84
7.30 53.29
7.40 54.76
7.50 56.25
7.60 57.76
7.70 59.29
7.80 60.84
7.90 62.41
8.00 64.00
8.10 65.61
8.20 67.24
8.30 68.89
8.40 70.56
8.50 72.25
8.60 73.96
8.70 75.69
8.80 77.44
8.90 79.21
9.00 81.00
9.10 82.81
9.20 84.64
9.30 86.49
9.40 88.36
9.50 90.25
9.60 92.16
9.70 94.09
9.80 96.04
9.90 98.01
10.00 100.00
请按任意键继续. . .