首先编写一个python脚本,命名为pytest.py;
1 defadd(a,b):2 print "in python function add"
3 return a+b
c调用python示例:
1 #include
2 #include
3 //#include "C:/Python27/include/python.h"4 //#pragma comment(lib, "C:\\Python27\\libs\\python27.lib")5 //若在编译器设置了include和lib路径以上两行可以转化为
6 #include
7 int main(char argc,char **argv)8 {9 //初始化Python10 //在使用Python系统前,必须使用Py_Initialize对其进行初始化。11 //它会载入Python的内建模块并添加系统路径到模块搜索路径中。
12 PyObject *pName,*pModule,*pDict,*pFunc,*pArgs,*pRetVal;13 Py_Initialize();14 if(!Py_IsInitialized())15 {16 return -1;17 }18 //载入名为pytest的脚本,(注意:不是pytest.py)
19 pName = PyString_FromString("pytest");20 pModule =PyImport_Import(pName);21 if(!pModule)22 {23 printf("can't find pytest.py");24 getchar();25 return -1;26 }27 pDict =PyModule_GetDict(pModule);28 if(!pDict)29 {30 return -1;31 }32 //找出函数名为add的函数
33 pFunc = PyDict_GetItemString(pDict,"add");34 if(!pFunc || !PyCallable_Check(pFunc))35 {36 printf("can't find function [add]");37 getchar();38 return -1;39 }40 //参数进栈
41 pArgs = PyTuple_New(2);42 //下面的是重点了,从c/c++传参数到Python中时需要用到一个函数,原型为43 //PyObject* Py_BuildValue(char *format,……)44 //类似c的printf,但格式不同。常见的格式有45 //s 字符串46 //i 整形变量47 //f 浮点型48 //o Python对象
49 PyTuple_SetItem(pArgs,0,Py_BuildValue("l",3));50 PyTuple_SetItem(pArgs,1,Py_BuildValue("l",4));51 //调用Python函数
52 pRetVal =PyObject_CallObject(pFunc,pArgs);53 printf("function return value: %ld\r\n",PyInt_AsLong(pRetVal));54 Py_DECREF(pName);55 Py_DECREF(pArgs);56 Py_DECREF(pModule);57 Py_DECREF(pRetVal);58 //关闭Python
59 Py_Finalize();60 return 0;61 }
编译运行出错:
加了include和lib还是链接出错,比如 canno open file "python27.lib",直接在工程->设置->链接上加上python27.lib
"s" (string) [char *] :将C字符串转换成Python对象,如果C字符串为空,返回NONE。
"s#" (string) [char *, int] :将C字符串和它的长度转换成Python对象,如果C字符串为空指针,长度忽略,返回NONE。
"z" (string or None) [char *] :作用同"s"。
"z#" (string or None) [char *, int] :作用同"s#"。
"i" (integer) [int] :将一个C类型的int转换成Python int对象。
"b" (integer) [char] :作用同"i"。
"h" (integer) [short int] :作用同"i"。
"l" (integer) [long int] :将C类型的long转换成Pyhon中的int对象。
"c" (string of length 1) [char] :将C类型的char转换成长度为1的Python字符串对象。
"d" (float) [double] :将C类型的double转换成python中的浮点型对象。
"f" (float) [float] :作用同"d"。
"O&" (object) [converter, anything] :将任何数据类型通过转换函数转换成Python对象,这些数据作为转换函数的参数被调用并且返回一个新的Python对象,如果发生错误返回NULL。
"(items)" (tuple) [matching-items] :将一系列的C值转换成Python元组。
"[items]" (list) [matching-items] :将一系列的C值转换成Python列表。
"{items}" (dictionary) [matching-items] :将一系类的C值转换成Python的字典,每一对连续的C值将转换成一个键值对。
例如:
Py_BuildValue("") None
Py_BuildValue("i", 123) 123
Py_BuildValue("iii", 123, 456, 789) (123, 456, 789)
Py_BuildValue("s", "hello") 'hello'
Py_BuildValue("ss", "hello", "world") ('hello', 'world')
Py_BuildValue("s#", "hello", 4) 'hell'
Py_BuildValue("()") ()
Py_BuildValue("(i)", 123) (123,)
Py_BuildValue("(ii)", 123, 456) (123, 456)
Py_BuildValue("(i,i)", 123, 456) (123, 456)
Py_BuildValue("[i,i]", 123, 456) [123, 456]Py_BuildValue("{s:i,s:i}","abc", 123, "def", 456) {'abc': 123, 'def': 456}
Py_BuildValue("((ii)(ii)) (ii)",1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6))