gpt4 book ai didi

c++ - 在 Cython 中包装 C++ 时是否必须递归包含所有 header

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:27:19 25 4
gpt4 key购买 nike

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

header 本身包括其他库来定义类型(实际上,相当多),像这样:

#include <Eigen/Dense>

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

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

最佳答案

我不确定您的要求是什么,但据我了解,我会说:只需公开您需要的内容(此处为 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

完成上述包装后,您可以在 Cython 代码中的任何需要的地方自由使用 State

# 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)
...

例子:

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

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 中的单个 block (没有额外的 .pyd)。

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

关于c++ - 在 Cython 中包装 C++ 时是否必须递归包含所有 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17304936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com