日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python直接調用和使用swig法方調用c++庫_python

作者:LordofRobots ? 更新時間: 2022-05-04 編程語言

c++運算速度快于python,python簡單易寫。很多時候對于已有的c++代碼也不想用python重寫,此時就自然而然地想到用python調用c或者c++,兩全其美。
然而根據這些博客的說法,python只能實現c的調用,如果需要調用c++,還需要對c++代碼進行額外的處理。

首先是python調用c代碼:

//gcc -g -o libpycall_c.so -shared -fPIC pycall_c.c
#include ?
#include ?
int foo(int a, int b)?
{?
? printf("you input %d and %d\n", a, b);?
? return a+b;?
}?

此處一定要用gcc進行編譯,,如果用g++就搞成c++了,python不能直接調用c++!(我在這里報錯了很久,因為我用的是g++)

import ctypes?
lib = ctypes.CDLL("./libpycall_c.so") ??
lib.foo(1, 3)?
print '***finish***'

可見python調用c的方式還是很直接的。當調用c++時,使用g++編譯生成C動態庫的代碼中的函數或者方法,需要使用extern “C”來進行編譯。

//g++ -g -o libpycall.so -shared -fPIC pycall.c
#include 
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***'

更高級一點,c++定義一個類,通過python調用c++類的方法。

首先寫一個c++類:

//g++ -g -o libpycall.so -shared -fPIC -std=c++11 pycall.cpp
#include 

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

swig

Swig是一種軟件開發工具,能讓一些腳本語言調用C/C++語言的接口。它實現的方法是,通過編譯程序將C/C++的聲明文件(.i文件)編譯成C/C++的包裝器源代碼(.c或.cxx)。通過直接調用這樣的包裝器接口,腳本語言可以間接調用C/C++語言的程序接口。SWIG支持的語言有:Perl, Python, Tcl, Ruby, Guile, and Java。

假如有這樣一段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);
}

我們想在腳本語言的代碼里面調用fact函數。可以通過一段非常簡單的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腳本的一個命令,它表示生成的包裝器將在一個模塊內的名稱。
  • 第二部分是%{… %},這一部分的內容會原封不動的插入到xxxx_wrap.c或xxxx_wrap.cxx文件中。
  • 第三部分就是剩下的部分了。這部分就是C語言或者C++語言的接口聲明了。和C/C++的語法是一樣的。

接下來以linux操作系統下,為python語言生成接口為例:

swig -python example.i

執行上述語句會生成兩個文件example.py和example_wrap.c。 example.py就是python語言可以調用的example模塊,而example_wrap.c則封裝了example.c的封裝器。

然后執行第二步:

gcc -c -fPIC example.c example_wrap.c -I/usr/include/python2.7

執行該步會生成兩個o文件,example.oexample_wrap.o

最后執行:

g++ -shared example.o example_wrap.o -o _example.so

這一步會將上面兩個o文件封裝成一個新的動態庫,_example.so。在這之后就可以在python內直接調用example.c提供的接口了。

import example
print example.fact(3)
print example.cvar.My_variable ? #注意這里的參數不能直接用,得用cvar。

原文鏈接:https://blog.csdn.net/LordofRobots/article/details/77870862

欄目分類
最近更新