python?與c++相互調(diào)用實現(xiàn)
一、c++調(diào)用Python
將Python安裝目錄下的include
和libs
文件夾引入到項目中,將libs目錄下的python37.lib復(fù)制一份為python37_d.lib
1.Python腳本
def Hello(): ? ? print("Hello") ? ? ? def Add(a,b): ? ? return ?a+b
2.C++調(diào)用python腳本
#include <Python.h> using namespace std; ? int main() { ? ? Py_Initialize(); ? ? ? ? ? ? ?//初始化,創(chuàng)建一個Python虛擬環(huán)境 ? ? if (Py_IsInitialized()) ? ? { ? ? ? ? PyObject* pModule = NULL; ? ? ? ? PyObject* pFunc = NULL; ? ? ? ? pModule = PyImport_ImportModule("test_python"); ?//參數(shù)為Python腳本的文件名 ? ? ? ? if (pModule) ? ? ? ? { ? ? ? ? ? ? pFunc = PyObject_GetAttrString(pModule, "Hello"); ? //獲取函數(shù) ? ? ? ? ? ? PyEval_CallObject(pFunc, NULL); ? ? ? ? ? //執(zhí)行函數(shù) ? ? ? ? } ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? printf("導(dǎo)入Python模塊失敗...\n"); ? ? ? ? } ? ? } ? ? else ? ? { ? ? ? ? printf("Python環(huán)境初始化失敗...\n"); ? ? } ? ? Py_Finalize(); }
二、接口方法
Python3.6提供給C/C++接口函數(shù),基本都是定義pylifecycle.h
,pythonrun.h
,ceval.h
中。
- Py_Initialize() 和 Py_Finalize():必須先調(diào)用Py_Initialize()進(jìn)行初始化,這個API用來分配Python解釋器使用的全局資源,應(yīng)用程序結(jié)束時需要調(diào)用Py_Finalize()來關(guān)閉Python的使用環(huán)境。
- Py_IsInitialized():用來判斷Python解釋器是否初始化成功,true為成功,false為失敗。
- PyErr_Print() & PyErr_Clear():執(zhí)行Python出錯時,PyErr_Print()可將錯誤信息顯示出來,PyErr_Clear()將錯誤信息在Python解釋器的緩存清除。
- PyRun_SimpleString():這個函數(shù)能夠用來執(zhí)行簡單的Python語句。
- PyEval_InitThreads():如果使用多線程調(diào)用Python腳本,就需要在初始化Python解釋器時調(diào)用PyEval_InitThreads()來啟用線程支持(導(dǎo)致Python內(nèi)部啟用線程鎖),最好在主線程啟動時就調(diào)用。該API同時也鎖定全局解釋鎖,所以,還需要在初始化完成后需要自行釋放鎖。
- 如果不需要使用多線程,不建議啟用該選項,互斥鎖也會不可避免的增加系統(tǒng)開銷。
1.規(guī)范化語法
#include<Python.h> //添加python的聲明 ? using namespace std; ? int main() { Py_Initialize(); //1、初始化python接口 ? //初始化使用的變量 PyObject* pModule = NULL; PyObject* pFunc = NULL; PyObject* pName = NULL; ? //2、初始化python系統(tǒng)文件路徑,保證可以訪問到 .py文件 PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); ? //3、調(diào)用python文件名。當(dāng)前的測試python文件名是test.py。在使用這個函數(shù)的時候,只需要寫文件的名稱就可以了。不用寫后綴。 pModule = PyImport_ImportModule("test"); ? //4、調(diào)用函數(shù) pFunc = PyObject_GetAttrString(pModule, "AdditionFc"); ? //5、給python傳參數(shù) PyObject* pArgs = PyTuple_New(2);//函數(shù)調(diào)用的參數(shù)傳遞均是以元組的形式打包的,2表示參數(shù)個數(shù)。如果AdditionFc中只有一個參數(shù)時,寫1就可以了。這里只先介紹函數(shù)必須有參數(shù)存在的情況。 ? ? PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 2)); //0:表示序號。第一個參數(shù)。 PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 4)); //1:也表示序號。第二個參數(shù)。i:表示傳入的參數(shù)類型是int類型。 ? //6、使用C++的python接口調(diào)用該函數(shù) PyObject* pReturn = PyEval_CallObject(pFunc, pArgs); ? //7、接收python計算好的返回值 int nResult; PyArg_Parse(pReturn, "i", &nResult);//i表示轉(zhuǎn)換成int型變量。在這里,最需要注意的是:PyArg_Parse的最后一個參數(shù),必須加上“&”符號。 ? //8、結(jié)束python接口初始化 Py_Finalize(); }
三、Pthon調(diào)用c++
python調(diào)用c++一種是基于extern 的方式,另一種是swig
1.基于extern
初級版:
首先先看一下Python調(diào)用c
C代碼:
#include <stdio.h>? #include <stdlib.h>? int foo(int a, int b)? {? ? printf("you input %d and %d\n", a, b);? ? return a+b;? }?
Python代碼:
import ctypes? lib = ctypes.CDLL("./libpycall_c.so") ?? lib.foo(1, 3)? print '***finish***'
編譯:
gcc -g -o libpycall_c.so -shared -fPIC pycall_c.c
然后基于c++改造上述代碼(使用g++編譯生成C動態(tài)庫的代碼中的函數(shù)或者方法,需要使用extern “C”來進(jìn)行編譯)
c++代碼:
#include <iostream> using namespace std; int foo(int a, int b){ ? ? cout << "the number you input:" << a << "\t" << b << endl; ? ? return a + b; } extern "C" { ? ?int foo_(int a, int b){ ? ? ? ?foo(a, b); ? ? ? } }
python代碼:
import ctypes? lib = ctypes.CDLL("./libpycall.so") ?? lib.foo_(1, 3)? print '***finish***'
編譯:
g++ -g -o libpycall.so -shared -fPIC pycall.cpp
升級版:
c++定義一個類,通過python調(diào)用c++類的方法
#include <iostream> using namespace std; class TestLib{ ? ? private: ? ? ? ? int number = 0; ? ? public: ? ? ? ? void set_number(int num){ ? ? ? ? ? ? number = num; ? ? ? ? } ? ? ? ? int get_number(){ ? ? ? ? ? ? return number; ? ? ? ? } };? extern "C" { ? ? TestLib obj; ? ? int get_number(){ ? ? ? ? return obj.get_number(); ? ? } ? ? void set_number(int num){ ? ? ? ? obj.set_number(num); ? ? } }
python 代碼:
import ctypes lib = ctypes.CDLL("./libpycall.so") print lib.get_number() ?#0 lib.set_number(10) print lib.get_number() ? #10
編譯:
g++ -g -o libpycall.so -shared -fPIC -std=c++11 pycall.cpp
2.基于swig
Swig
是一種軟件開發(fā)工具,能讓一些腳本語言調(diào)用C/C++語言的接口。它實現(xiàn)的方法是,通過編譯程序?qū)/C++的聲明文件(.i文件)編譯成C/C++的包裝器源代碼(.c或.cxx)。通過直接調(diào)用這樣的包裝器接口,腳本語言可以間接調(diào)用C/C++語言的程序接口。
參考地址:https://github.com/swig/swig
首先安裝,源碼或者pip
案例:
有這樣一段C的代碼,文件名為example.c
/* File : example.c */ double ?My_variable ?= 3.0; /* Compute factorial of n */ int ?fact(int n) { ? ? if (n <= 1) return 1; ? ? else return n*fact(n-1); } /* Compute n mod m */ int my_mod(int n, int m) { ? ? return(n % m); }
你想在你的腳本語言的代碼里面調(diào)用fact函數(shù)。你可以通過一段非常簡單的SWIG腳本,文件名為example.i:(這里的格式非常重要,即使第一行的注釋也不能省略)
/* File : example.i */ %module example %{ /* Put headers and other declarations here */ extern double My_variable; extern int ? ?fact(int); extern int ? ?my_mod(int n, int m); %} extern double My_variable; extern int ? ?fact(int); extern int ? ?my_mod(int n, int m);
這段.i文件分成3個部分:
- 第一部分是
%module example
,%module
是SWIG腳本的一個命令,它表示生成的包裝器將在一個模塊內(nèi)的名稱。 - 第二部分是%{… %},這一部分的內(nèi)容會原封不動的插入到
xxxx_wrap.c
或xxxx_wrap.cxx
文件中。 - 第三部分就是剩下的部分了。這部分就是C語言或者C++語言的接口聲明了。和C/C++的語法是一樣的。
接下來以linux操作系統(tǒng)下,為python語言生成接口為例:
swig -python example.i
執(zhí)行上述語句會生成兩個文件example.py
和example_wrap.c
。example.py
就是python語言可以調(diào)用的example模塊,而example_wrap.c則封裝了example.c
的封裝器。
然后執(zhí)行第二步:
gcc -c -fPIC example.c example_wrap.c -I/usr/include/python2.7
執(zhí)行該步會生成兩個o文件,example.o
和example_wrap.o
。
最后執(zhí)行:
g++ -shared example.o example_wrap.o -o _example.so
這一步會將上面兩個o文件封裝成一個新的動態(tài)庫,_example.so
。在這之后就可以在python內(nèi)直接調(diào)用example.c提供的接口了。
import example print example.fact(3) print example.cvar.My_variable ? #注意這里的參數(shù)不能直接用,得用cvar
到此這篇關(guān)于python 與c++相互調(diào)用實現(xiàn)的文章就介紹到這了,更多相關(guān)python 與c++相互調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python開發(fā)SQLite3數(shù)據(jù)庫相關(guān)操作詳解【連接,查詢,插入,更新,刪除,關(guān)閉等】
這篇文章主要介紹了Python開發(fā)SQLite3數(shù)據(jù)庫相關(guān)操作,結(jié)合實例形式較為詳細(xì)的分析了Python操作SQLite3數(shù)據(jù)庫的連接,查詢,插入,更新,刪除,關(guān)閉等相關(guān)操作技巧,需要的朋友可以參考下2017-07-07python 使用turtule繪制遞歸圖形(螺旋、二叉樹、謝爾賓斯基三角形)
這篇文章主要介紹了python 使用turtule繪制遞歸圖形(螺旋、二叉樹、謝爾賓斯基三角形) ,需要的朋友可以參考下2019-05-05