我们在Python进阶教程(二),介绍了一些Python进阶用法。今天给大家介绍的是和c/c++混合编程的用法。我们都知道特别是Python2.x版本GIL的影响造成多线程计算方面是鸡肋,并且常常在高性能计算方面依赖很多包。如果我们想切合我们自己的业务处理,比如高性能部分我们用C/C++生成动态链接库,Python调用该动态链接库。我们今天主要介绍Python在和c/c++混合编程。
在和C/C++代码进行混合编程时,我们通常需要采用以下几个技术点:
我们通过一个C的源代码函数,我们来看一下,我们新建一个C函数文件叫做test.c。比如:
#include <stdio.h>
int add_int(int, int);
float add_float(float, float);
int add_int(int x, int y)
{
return x + y;
}
float add_float(float x, float y)
{
return x + y;
}
然后将该test.c文件生成动态链接库add.so,通过如下命令:
gcc -shared -Wl,-install_name,add.so -o add.so -fPIC test.c
-shared代表这是动态库,-fPIC使得位置独立,-o指定了输出文件,改成dll后缀一样可以用。可以在当前目录看到有一个文件add.so。
from ctypes import *
#load the shared object file
adder = CDLL('./add.so')
#Find sum of integers
res_int = adder.add_int(4,5)
print "Sum of 4 and 5 = " + str(res_int)
#Find sum of floats
a = c_float(5.5)
b = c_float(4.1)
add_float = adder.add_float
#这个非常重要,如果你没有给返回值指定输出Python ctypes的数据类型则会出现异常值。
add_float.restype = c_float
print "Sum of 5.5 and 4.1 = ", str(add_float(a, b))
#输出为
Sum of 4 and 5 = 9
Sum of 5.5 and 4.1 = 10.6000003815
在指定ctypes的数据格式时一定要注意数据类型的匹配test1。我们来看一下数据格式的匹配test:
更详细的使用ctypes教程ctypes tutorial。
ctypes不仅可以和C在一混合编程,我们还可以和C++混合编程,但是在ctypes的官方文档里面只定义了支持C Type。那么c++如何和Python进行混合编程呢?所以需要在C++里面通过extend c来支持C++混合编程。 我们来看两个例子,第一个例子由两个文件构成一个是test.h,test.cpp,其中test.h是c++自定义的头文件,test.cpp是对外接口。依次来看看:
#test.h
#ifndef _Foo_h
#define _Foo_h
#include <iostream>
class Foo
{
public:
Foo(int);
void bar();
int foobar(int);
private:
int val;
};
Foo::Foo(int n)
{
val = n;
}
void Foo::bar()
{
std::cout << "Value is " << val << std::endl;
}
int Foo::foobar(int n)
{
return val + n;
}
#endif // !_Foo_h
我们来看一下它的对外接口test.cpp是什么样的?
#include "test.h"
//值得注意的是extern "C"
extern "C" {
Foo *Foo_new(int n)
{
return new Foo(n);
}
void Foo_bar(Foo *foo)
{
foo->bar();
}
int Foo_foobar(Foo *foo, int n)
{
return foo->foobar(n);
}
}
这个例子非常不错不仅有对象,指针和普通参数。这个我是通过cmake和make来进行编译和处理的,我们看一下生成动态链接库。
cmake_minimum_required(VERSION 3.0)
project (Demo)
set (SOURCE test.cpp)
add_library(${PROJECT_NAME} SHARED ${SOURCE})
然后执行下面两条命令
cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ..
make -j 8
#在当前目录就会出现下面两个命令
libDemo.dylib
我们来看一下Python是如何调用该接口的。
import ctypes
lib = ctypes.cdll.LoadLibrary("./libDemo.dylib")
class Foo(object):
def __init__(self, val):
lib.Foo_new.argtypes = [ctypes.c_int]
lib.Foo_new.restype = ctypes.c_void_p
lib.Foo_bar.argtypes = [ctypes.c_void_p]
lib.Foo_bar.restype = ctypes.c_void_p
lib.Foo_foobar.argtypes = [ctypes.c_void_p, ctypes.c_int]
lib.Foo_foobar.restype = ctypes.c_int
self.obj = lib.Foo_new(val)
def bar(self):
lib.Foo_bar(self.obj)
def foobar(self, val):
return lib.Foo_foobar(self.obj, val)
f=Foo(5)
f.bar()
print (f.foobar(7))
x = f.foobar(2)
print (type(x))
#输出
12
<type 'int'>
我们来看另外一个例子,也是通过动态链接库来处理的。
#include <iostream>
#include <string>
using namespace std;
typedef struct StructTest
{
char *name;
int age;
int score[3];
} StructTest, *StructPtr;
class TestLib
{
public:
int passInt(int a);
double passDouble(double d);
char passChar(char c);
char *passString(char *s);
StructTest passStruct(StructTest st);
StructPtr passStructPtr(StructPtr p);
StructPtr passStructArray(StructTest vst[], int size);
};
int TestLib::passInt(int a)
{
cout << a << " in c++" << endl;
return a;
}
double TestLib::passDouble(double d)
{
cout << d << " in c++" << endl;
return d;
}
char TestLib::passChar(char c)
{
cout << c << " in c++" << endl;
return c;
}
char *TestLib::passString(char *s)
{
cout << s << " in c++" << endl;
return s;
}
StructTest TestLib::passStruct(StructTest st)
{
cout << st.name << " " << st.age << " " << st.score[2] << " in c++" << endl;
return st;
}
StructPtr TestLib::passStructPtr(StructPtr p)
{
cout << p->name << " " << p->age << " " << p->score[2] << " in c++" << endl;
return p;
}
StructPtr TestLib::passStructArray(StructTest vst[], int size)
{
cout << vst[0].name << " in c++" << endl;
cout << vst[1].name << " in c++" << endl;
return &vst[0];
}
extern "C" {
TestLib obj;
int passInt(int a)
{
return obj.passInt(a);
}
double passDouble(double d)
{
return obj.passDouble(d);
}
char passChar(char c)
{
return obj.passChar(c);
}
char *passString(char *s)
{
return obj.passString(s);
}
StructTest passStruct(StructTest st)
{
return obj.passStruct(st);
}
StructPtr passStructPtr(StructPtr p)
{
return obj.passStructPtr(p);
}
StructPtr passStructArray(StructTest vst[], int size)
{
return obj.passStructArray(vst, size);
}
}
我们来看一下python相应的代码,改代码实例来自[参考2],我们看看Python是如何调用的?
import ctypes
lib = ctypes.cdll.LoadLibrary('./libTest.so')
# passInt
print('passInt')
print(lib.passInt(100))
print('--------------------')
# passDouble
print('passDouble')
lib.passDouble.restype = ctypes.c_double
print(str(lib.passDouble(ctypes.c_double(1.23))) + ' in python')
print('--------------------')
# passChar
print('passChar')
lib.passChar.restype = ctypes.c_char
print(str(lib.passChar(ctypes.c_char(65))) + ' in python') # 'A'
print(str(lib.passChar(ctypes.c_char(b'A'))) + ' in python') # 'A'
print('--------------------')
# passString
print('passString')
lib.passString.restype = ctypes.c_char_p
print(str(lib.passString(ctypes.c_char_p(b'abcde'))) + ' in python') # 这里一定要加b
print('--------------------')
# passStruct
print('passStruct')
class Struct(ctypes.Structure):
_fields_ = [('name', ctypes.c_char_p),
('age', ctypes.c_int),
('score', ctypes.c_int * 3)]
lib.passStruct.restype = Struct
array = [1, 2, 3]
st = lib.passStruct(Struct(b'xidui', 10, (ctypes.c_int * 3)(*array)))
# p = lib.passStruct(Struct(b'xidui', 10, (ctypes.c_int * 3)(1, 2, 3)))
print(str(st.name) + ' ' + str(st.age) + ' ' + str(st.score[2]) + ' in python')
print('--------------------')
# passStructPointer
print('passStructPointer')
lib.passStructPtr.restype = ctypes.POINTER(Struct)
lib.passStructPtr.argtypes = [ctypes.POINTER(Struct)] # 这行不加,程序会宕
p = lib.passStructPtr(Struct(b'xidui', 10, (ctypes.c_int * 3)(*array)))
print(str(p.contents.name) + ' ' + str(p.contents.age) + ' ' + str(p.contents.score[2]) + ' in python')
print('--------------------')
# passStructArray
print('passStructArray')
lib.passStructArray.restype = ctypes.POINTER(Struct)
lib.passStructArray.argtypes = [ctypes.ARRAY(Struct, 2), ctypes.c_int]
array = [Struct(b'xidui1', 10, (ctypes.c_int * 3)(1, 2, 3)),
Struct(b'xidui2', 10, (ctypes.c_int * 3)(1, 2, 3))]
p = lib.passStructArray(ctypes.ARRAY(Struct, 2)(*array), 2)
print(str(p.contents.name) + ' ' + str(p.contents.age) + ' ' + str(p.contents.score[2]) + ' in python')
print('--------------------')
1.Calling C++ Classes from Python with ctypes 1 2.Calling C++ Classes from Python with ctypes 2