先堆(好像没什么用的)参考资料 C++和python的代码如何相互调用? 浅谈python中使用C/C++:ctypes How to call C / C++ from Python? https://docs.python.org/3.11/library/ctypes.html MingW下动态链接库常识 MinGW gcc 生成动态链接库 dll 的一些问题汇总 Python调用C++ 传数组指针参数
Linux下我试了没问题的 python3调用cpp的方法——python调用so Windows下用mingw一直有问题,没找到解决方法。先记vs的。
visual studio
新建动态链接库项目 或在项目属性里改 关闭预编译头 复制粘贴以下代码。代码还是来自上面提到的那篇博客 python3调用cpp的方法——python调用so,针对MSVC稍作修改。vs给的示例程序没用上。
#include <iostream>
class Test {
private:
double _calculate(int a, double b);
public:
double calculate(int a, double b, char c[], int* d, double* e, char** f);
};
double Test::_calculate(int a, double b) {
double res = a + b;
std::cout << "res: " << res << std::endl;
return res;
}
double Test::calculate(int a, double b, char c[], int* d, double* e, char** f) {
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
std::cout << "c: " << c << std::endl;
std::cout << "d: " << d[0] << d[1] << std::endl;
std::cout << "e: " << e[0] << e[1] << std::endl;
std::cout << "f: " << f[0] << f[1] << std::endl;
return this->_calculate(a, b);
}
extern "C" _declspec(dllexport) Test* test_new()
{
return new Test;
}
extern "C" _declspec(dllexport) double my_calculate(Test* t, int a, double b, char c[], int* d, double* e, char** f)
{
return t->calculate(a, b, c, d, e, f);
}
编译时注意选release和x64平台。原因:解决Python调试OSError: [WinError 193] %1 不是有效的 Win32 应用程序。release是因为都生成库了也没必要调试了。将编译后生成的dll文件重命名为test.dll。
import ctypes
lib = ctypes.cdll.LoadLibrary('./test.dll')
lib.my_calculate.restype = ctypes.c_double
class Test(object):
def __init__(self):
self.obj = lib.test_new()
def calculate(self, a, b, c, d, e, f):
res = lib.my_calculate(self.obj, a, b, c, d, e, f)
return res
def convert_type(data):
ctypes_map = {int: ctypes.c_int,
float: ctypes.c_double,
str: ctypes.c_char_p
}
input_type = type(data)
if input_type is list:
length = len(data)
if length == 0:
print("convert type failed...input is " + data)
return None
else:
arr = (ctypes_map[type(data[0])] * length)()
for i in range(length):
arr[i] = bytes(data[i], encoding="utf-8") if (type(data[0]) is str) else data[i]
return arr
else:
if input_type in ctypes_map:
return ctypes_map[input_type](bytes(data, encoding="utf-8") if type(data) is str else data)
else:
print("convert type failed...input is " + data)
return None
if __name__ == '__main__':
t = Test()
A1 = 123
A2 = 0.789
A3 = "C789"
A4 = [456, 789]
A5 = [0.123, 0.456]
A6 = ["A123", "B456"]
print(t.calculate(convert_type(A1), convert_type(A2), convert_type(A3), convert_type(A4), convert_type(A5), convert_type(A6)))
一个最简单的例子
class Test {
public:
double mysum(double a, double b) { return a + b; };
};
extern "C" _declspec(dllexport) double my_calculate(double a, double b)
{
Test t;
return t.mysum(a, b);
}
import ctypes
lib = ctypes.cdll.LoadLibrary('./test.dll')
lib.my_calculate.restype = ctypes.c_double
A1 = 123
A2 = 0.789
print(lib.my_calculate(ctypes.c_double(A1), ctypes.c_double(A2)))
一个结构体的例子
Calling C functions from Python - part 1 - using ctypes
typedef struct{
double x, y, z;
}Vector3d;
extern "C" _declspec(dllexport) Vector3d Vec_Add(Vector3d a, Vector3d b)
{
Vector3d ans;
ans.x = a.x + b.x;
ans.y = a.y + b.y;
ans.z = a.z + b.z;
return ans;
}
import ctypes
lib = ctypes.cdll.LoadLibrary('./test.dll')
class Test(ctypes.Structure):
_fields_ = [("j1", ctypes.c_double), ("j2", ctypes.c_double), ("j3", ctypes.c_double)]
lib.Vec_Add.restype = Test
vectora = Test(1, 2, 3.5)
vectorb = Test(1.5, 2.5, 4.5)
vectorc = lib.Vec_Add(vectora, vectorb)
print(vectorc.j1, vectorc.j2, vectorc.j3)
|