私信  •  关注

Garfield

Garfield 最近回复了
4 年前
回复了 Garfield 创建的主题 » 如何在Python中使用C库?

我喜欢CPpyy,它非常容易用C++代码扩展Python,在需要时显著提高性能。

这里是一个示例,说明如何创建一个NUMPY数组并将其传递给C++中的类成员函数。

import cppyy
cppyy.add_include_path("include")
cppyy.include('mylib/Buffer.h')


s = cppyy.gbl.buffer.Buffer()
numpy_array = np.empty(32000, np.float64)
s.get_numpy_array(numpy_array.data, numpy_array.size)

在C++中:

struct Buffer {
  void get_numpy_array(int beam, double *ad, int size) {
    // fill the array
  }
}