语句调用和模块调用两种方式代码sample,部分语句python3与python2.7有所不同,本文按照python3.5编写
语句调用
test4.cpp
//g++ -I c:\ProgramData\Anaconda3\include -L c:\ProgramData\Anaconda3\libs test4.cpp -lpython37 -o test4.exe
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize(); // 初始化
PyObject *t;
t = PyTuple_New(3);
if (t==NULL) {
printf(“t=NULL”);
return 0;
}
PyTuple_SetItem(t, 0, PyLong_FromLong(1L));
PyTuple_SetItem(t, 1, PyLong_FromLong(2L));
PyTuple_SetItem(t, 2, PyUnicode_FromString(“three”));
printf(“test4”);
Py_DECREF(t);
Py_Finalize();
return 0;
}
模块调用
pytest.py
def test_add(a, b):
print('add ', a, ’ and ', b)
return a+b