3

我正在尝试使用 Cython 包装一个 C++ 库。

标头本身包括其他库来定义类型(实际上,很多),如下所示:

#include <Eigen/Dense>

namespace abc {
namespace xyz {
typedef Eigen::Matrix<double, 5, 1> Column5;
typedef Column5 States;
}}

有很多“外部”类型定义。有没有办法为 Eigen 库编写一个 .pxd 文件?我只需要 .pxd 文件中可用的类型“States”来导入(包装)类定义......

4

1 回答 1

2

我不确定您要什么,但据我了解,我会说:只需公开您需要的内容(此处为State类型)。

在您的.pyx或您的.pxd

(假设您问题中的代码是名为 的文件的一部分my_eigen.h

# Expose State 
cdef extern from "some_path/my_eigen.h" namespace "xyz" :
    cdef cppclass States:
        # Expose only what you need here
        pass

完成上述包装后,您State可以在 Cython 代码中随意使用它。

# Use State as is in your custom wrapped class
cdef extern from "my_class_header.h" namespace "my_ns" :
    cdef cppclass MyClassUsingStates:
        double getElement(States s, int row, int col)
        ...

例子:

这里我的需要是:拥有一个 python 处理程序,std::ofstream并且不需要公开它的任何方法(所以我没有公开任何方法,但它本来是可能的......)

cdef extern from "<fstream>" namespace "std" :
    cdef cppclass ofstream:
        pass

cdef class Py_ofstream:
    cdef ofstream *thisptr

    def __cinit__(self):
       self.thisptr = new ofstream()

    def __dealloc__(self):
       if self.thisptr:
           del self.thisptr

注意:这里我直接将它用作我的单个块.pyx(没有附加.pyd)。

如果误解了问题,请告诉我...

于 2013-06-26T13:01:58.463 回答