詳解python如何調用C/C++底層庫與互相傳值,詳解python
前言
開發環境:
Centos 7 + Python 3.5.1 + Qt Creator(只是使用Qt Creator編譯而已,並沒有使用QT的任何庫)
Python調用C/C++庫,我現在能做到的有兩種方式
1.extern “C” 匯出(互相傳值比較麻煩,不建議使用這種方式):
將C/C++庫做成和平常一樣的DLL和或者.so,比如:
//.h檔案#include <Python.h>//.cpp檔案//C/C++ my.so 或者my.dllenter "C" void printHello(){ std::cout<<"Hello World"<<std::endl;}
#Pythonimport ctypes from ctypes import *loadso = ctypes.cdll.LoadLibrary mylib = loadso("./my.so")mylib.printHello()>>>Hello world
代碼解釋:
my.so 有一個C匯出函數 printHello()
import ctypes : 匯入官方的一個庫,顧名思義和C有關
loadso = ctypes.cdll.LoadLibrary : loadso 表示載入庫用的函數
mylib = loadso(“./my.so”) //或者loadso(“my.dll”) 載入my.so庫
mylib.printHello() : 調用庫函數
上述代碼能正常輸出:Hello World,但是他們沒有互相傳值
Python和C++互相傳值
//.h檔案#include <Python.h>//.cpp檔案enter "C" int printHello(const char* str){ std::cout<<str<<std::endl; return 1; }
那麼Python的問題就來了
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#由於對Python不是特別的熟悉 怎麼也做不到顯示C++返回的字串, Python只能顯示C++返回的字串子能看到一個地址而已
2.Python擴充C/C++
不多說,直接上代碼
//.h檔案 本來這是C++串連Mysql 我只摘抄部分代#include <Python.h>//.cpp檔案//傳遞多個參數 Python傳過來的參數在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" 中可調用的函數 METH_VARARGS :帶有參數 METH_NOARGS:無參數"print"}, //說明{"connect", connect, METH_VARARGS,"connect mysql"},{NULL, NULL, 0, NULL}};static PyObject* UtilError;// 向Python中註冊模組static struct PyModuleDef spammodule = { PyModuleDef_HEAD_INIT,"libMysqlUtil", //模組名字 import libMysqlUtil"C++ Connect Mysql",-1,MyMethods};//PyInit_libMysqlUtil 注意名字 一定要PyInit_ 加上你的模組名字 不然Python import 會提示沒有定義 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; }
#pythonimport libMysqlUtillibMysqlUtil.printHello(1,"hello World")>>>1,hello World>>>OK
總結
到目前為止Python和C/C++互相通訊,能適應大部分需求,結構體傳值還沒有研究,對於類,使用指標就行,C++裡面是指標,在Python中會將指標轉化成整形,Python將這個整形傳給C++的時候使用PyArg_ParseTuple又將整形會變成類指標。
好了,以上就是本文的全部內容,希望本文的內容對大家學習python和C/C++能有所協助。