abstract:前言開(kāi)發(fā)環(huán)境:Centos 7 + Python 3.5.1 + Qt Creator(只是使用Qt Creator編譯而已,并沒(méi)有使用QT的任何庫(kù))Python調(diào)用C/C++庫(kù),我現(xiàn)在能做到的有兩種方式1.extern “C” 導(dǎo)出(互相傳值比較麻煩,不建議使用這種方式):將C/C++庫(kù)做成和平常一樣的DLL和或者.so,比如://.h文件 #include <Python.h&
前言
開(kāi)發(fā)環(huán)境:
Centos 7 + Python 3.5.1 + Qt Creator(只是使用Qt Creator編譯而已,并沒(méi)有使用QT的任何庫(kù))
Python調(diào)用C/C++庫(kù),我現(xiàn)在能做到的有兩種方式
1.extern “C” 導(dǎo)出(互相傳值比較麻煩,不建議使用這種方式):
將C/C++庫(kù)做成和平常一樣的DLL和或者.so,比如:
//.h文件 #include <Python.h> //.cpp文件 //C/C++ my.so 或者my.dll enter "C" void printHello() { std::cout<<"Hello World"<<std::endl; }
#Python import ctypes from ctypes import * loadso = ctypes.cdll.LoadLibrary mylib = loadso("./my.so") mylib.printHello() >>>Hello world
代碼解釋:
my.so 有一個(gè)C導(dǎo)出函數(shù) printHello()
import ctypes : 導(dǎo)入官方的一個(gè)庫(kù),顧名思義和C有關(guān)
loadso = ctypes.cdll.LoadLibrary : loadso 表示加載庫(kù)用的函數(shù)
mylib = loadso(“./my.so”) //或者loadso(“my.dll”) 加載my.so庫(kù)
mylib.printHello() : 調(diào)用庫(kù)函數(shù)
上述代碼能正常輸出:Hello World,但是他們沒(méi)有互相傳值
Python和C++互相傳值
//.h文件 #include <Python.h> //.cpp文件 enter "C" int printHello(const char* str) { std::cout<<str<<std::endl; return 1; }
那么Python的問(wèn)題就來(lái)了
str = create_string_buffer(b"Hello World") #mylib.printHello("Hello World") 這里死活就是顯示:H,*(str+4)才是'e',*(str+8) 是'l' 依次類推 print (mylib.printHello(str)) >>>Hello World >>>1 #由于對(duì)Python不是特別的熟悉 怎么也做不到顯示C++返回的字符串, Python只能顯示C++返回的字符串子能看到一個(gè)地址而已
2.Python擴(kuò)展C/C++
不多說(shuō),直接上代碼
//.h文件 本來(lái)這是C++連接Mysql 我只摘抄部分代#include <Python.h> //.cpp文件 //傳遞多個(gè)參數(shù) Python傳過(guò)來(lái)的參數(shù)在args里面 PyObject* printfHello(PyObject* self,PyObject* args) { int i=0 const char* str; if (!PyArg_ParseTuple(args, "i|s", &i,&str)) //i 表示整形 s 表示字符串 return PyLong_FromLong(0); print("%d,%s",i,str); return Py_BuildValue("s","OK"); //向Python返回OK字符串 } //映射 知道MFC的一看就懂 static PyMethodDef MyMethods[] = { {"printfHello", printfHello, METH_VARARGS, //"printHello" 中可調(diào)用的函數(shù) METH_VARARGS :帶有參數(shù) METH_NOARGS:無(wú)參數(shù) "print"}, //說(shuō)明 {"connect", connect, METH_VARARGS, "connect mysql"}, {NULL, NULL, 0, NULL} }; static PyObject* UtilError; // 向Python中注冊(cè)模塊 static struct PyModuleDef spammodule = { PyModuleDef_HEAD_INIT, "libMysqlUtil", //模塊名字 import libMysqlUtil "C++ Connect Mysql", -1, MyMethods };//PyInit_libMysqlUtil 注意名字 一定要PyInit_ 加上你的模塊名字 不然Python import 會(huì)提示沒(méi)有定義 PyInit_你的模塊名字 PyMODINIT_FUNC PyInit_libMysqlUtil(void) { PyObject* m = nullptr; m = PyModule_Create(&spammodule); //m= Py_InitModule(....) Python 2.7 if(!m) { return m; } UtilError = PyErr_NewException("Util.error",NULL,NULL); Py_INCREF(UtilError); PyModule_AddObject(m,"error",UtilError); return m; }
#python import libMysqlUtil libMysqlUtil.printHello(1,"hello World") >>>1,hello World >>>OK
總結(jié)
到目前為止Python和C/C++互相通信,能適應(yīng)大部分需求,結(jié)構(gòu)體傳值還沒(méi)有研究,對(duì)于類,使用指針就行,C++里面是指針,在Python中會(huì)將指針轉(zhuǎn)化成整形,Python將這個(gè)整形傳給C++的時(shí)候使用PyArg_ParseTuple又將整形會(huì)變成類指針。
更多關(guān)于python如何調(diào)用C/C++底層庫(kù)與互相傳值請(qǐng)關(guān)注PHP中文網(wǎng)(www.miracleart.cn)其他文章!