-
安装python-dev
sudo apt-get install python3-dev
-
查找Python的include目录
python3-config --cflags
-
启动cling指定Python的include目录和Python版本
cling -I/usr/include/python3.8 -L/usr/include/python3.8 -l python3.8
-
cling里面include Python.h
#include <Python.h>
-
初始化Python解释器
Py_Initialize()
-
执行Python指令
PyRun_SimpleString("print('hello C !')");
-
执行CPython指令
#include <object.h> #include <floatobject.h> PyObject *f = PyFloat_FromDouble(3.14); PyPrint(f, stdout, 0);
-
关闭Python解释器
Py_Finalize()