ctypes 调用动态链接库(.dll/.so)

  • Windows 系统中,在 Visual Studio 中新建一个动态链接库项目,本文将其命名为 lib4py,然后新建 lib4py.hlib4py.cpp 两个文件。
  • Linux 系统中,新建一个文件夹然后在文件夹中新建如上的两个文件。

动态链接库部分

lib4py.h 文件

#ifdef LIB4PY_EXPORTS
#define LIB4PY_API __declspec(dllexport)
#else
#define LIB4PY_API __declspec(dllimport)
#endif

extern "C"
{
    struct LIB4PY_API Person {
        char *name;
        int age;
        double *scores;
        int n_subjects;
    };

    struct LIB4PY_API Matrix {
        double **mat;
        int row;
        int col;
    };

    LIB4PY_API void print_info(Person *p);
    LIB4PY_API void print_mat(double **mat, int row, int col);
    LIB4PY_API Matrix* gen_mat(double **mat, int row, int col);
    LIB4PY_API void print_matrix(Matrix* m);
    LIB4PY_API void print_arr(double* arr, int n);
}

lib4py.cpp 文件

#include "lib4py.h"
#include 
#include 
using namespace std;

LIB4PY_API void print_info(Person *p) {
    cout << "Name: " << p->name << "\n" << "Age: " << p->age << "\n" << "scores: ";
    for (int i = 0; i < p->n_subjects; ++i)
        cout << p->scores[i] << " ";
    cout << endl;
}

LIB4PY_API void print_mat(double **mat, int row, int col) {
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j)
            cout << mat[i][j] << " ";
        cout << endl;
    }
}

LIB4PY_API Matrix* gen_mat(double **mat, int row, int col) {
    Matrix *m = (Matrix *)malloc(sizeof(Matrix));
    m->row = row;
    m->col = col;
    m->mat = mat;
    return m;
}

LIB4PY_API void print_matrix(Matrix* m) {
    for (int i = 0; i < m->row; ++i) {
        for (int j = 0; j < m->col; ++j)
            cout << m->mat[i][j] << " ";
        cout << endl;
    }
}
LIB4PY_API void print_arr(double* arr, int n) {
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}
  • Windows 系统中,直接编译后生成lib4py.dll
  • Linux 系统中,输入
    g++ lib4py.cpp lib4py.h -fPIC -shared -o lib4py.so
    
    命令生成 lib4py.so 文件。

然后在同文件夹下建立如下 Python 文件

Python 部分

from ctypes import *
import sys

# 加载动态链接库
if sys.platform == 'win32':
    _dll = CDLL("lib4py.dll")
else:
    _dll = CDLL("lib4py.so")


# 设置C函数的返回值类型和参数类型(如果类型是基本类型则不用调用该函数)
def fillprototype(f, restype, argtypes):
    f.restype = restype
    f.argtypes = argtypes

# 测试结构体
class Person(Structure):
    _fields_ = [("name", c_char_p),
                ("age", c_int),
                ("scores", POINTER(c_double)),
                ("n_subjects", c_int)]

class Matrix(Structure):
    _fields_ = [("mat", POINTER(POINTER(c_double))),
                ("row", c_int),
                ("col", c_int)]
    def __str__(self):
        return f""


# 一维数组
scores = (c_double * 5)(1, 2, 3, 4, 5)

print('Test array:')
_dll.print_arr(scores, 5)

print('Test 2-D array:')
arr1 = (c_double * 5)(1, 2, 3, 4, 5)
arr2 = (c_double * 5)(1, 2, 3, 4, 5)
# 二维数组
mat = (POINTER(c_double) * 2)(arr1, arr2)
_dll.print_mat(mat, 2, 5)

print('Test struct as args:')
p = Person(c_char_p(b"Liu"), 20, scores, 5)
_dll.print_info(byref(p))
matrix = Matrix(mat, 2, 5)
_dll.print_matrix(byref(matrix))

print('Test struct as return value:')
fillprototype(_dll.gen_mat, POINTER(Matrix), [POINTER(POINTER(c_double)), c_int, c_int])
matrix_p = _dll.gen_mat(mat, 2, 5)
print(matrix_p.contents)

输出

Test array:
1 2 3 4 5 
Test 2-D array:
1 2 3 4 5 
1 2 3 4 5 
Test struct as args:
Name: Liu
Age: 20
scores: 1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
Test struct as return value:

你可能感兴趣的:(ctypes 调用动态链接库(.dll/.so))