前言
開發環境:
Centos 7 + Python 3.5.1 + Qt Creator(只是使用Qt Creator編譯而已,并沒有使用QT的任何庫)
Python調用C/C++庫,我現在能做到的有兩種方式
1.extern “C” 導出(互相傳值比較麻煩,不建議使用這種方式):
將C/C++庫做成和平常一樣的DLL和或者.so,比如:
1
2
3
4
5
6
7
8
|
//.h文件 #include <Python.h> //.cpp文件 //C/C++ my.so 或者my.dll enter "C" void printHello() { std::cout<< "Hello World" <<std::endl; } |
1
2
3
4
5
6
7
|
#Python import 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++互相傳值
1
2
3
4
5
6
7
8
|
//.h文件 #include <Python.h> //.cpp文件 enter "C" int printHello( const char * str) { std::cout<<str<<std::endl; return 1; } |
那么Python的問題就來了
1
2
3
4
5
6
|
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++
不多說,直接上代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
//.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; } |
1
2
3
4
5
|
#python import libMysqlUtil libMysqlUtil.printHello( 1 , "hello World" ) >>> 1 ,hello World >>>OK |
總結
到目前為止Python和C/C++互相通信,能適應大部分需求,結構體傳值還沒有研究,對于類,使用指針就行,C++里面是指針,在Python中會將指針轉化成整形,Python將這個整形傳給C++的時候使用PyArg_ParseTuple又將整形會變成類指針。
好了,以上就是本文的全部內容,希望本文的內容對大家學習python和C/C++能有所幫助。