我在头文件 dcm.h 中有以下定义:
typedef struct
{
double alpha;
double gamma;
double tau;
} ThetaDCM;
我想将它导入到 cython 中,所以我有:
cdef extern from "dcm.h":
ctypedef struct ThetaDCM:
np.float64_t alpha
np.float64_t gamma
np.float64_t tau
现在我想将内存分配给 ThetaDCM 数组。我有以下内容:
cdef ThetaDCM *c_theta = <ThetaDCM *> malloc(nt * nb * sizeof(ThetaDCM))
free(c_theta)
这并没有编译并报告以下错误:
error: ‘ThetaDCM’ undeclared (first use in this function)
__pyx_v_c_theta = ((ThetaDCM *)malloc(((__pyx_v_nt * __pyx_v_nb) * (sizeof(ThetaDCM)))));
还有与此相关的其他错误。如果我在 extern 块之外定义 ThetaDCM,则代码编译不会出现问题。因此,如果我导入 Theta,cython 将看不到我的声明。有没有标准的方法来解决这个问题?
Edit:
我的文件标题比我发布的要复杂一些。它是
# ifdef __CUDACC__
# ifndef DDM_HEADER
# define DDM_HEADER
#include "math.h"
#include "curand_kernel.h"
#include "curand.h"
#include <stdio.h>
...
# endif
# ifdef __CUDACC__
# define BDDM_EXTERN extern "C"
# else
# define BDDM_DEVICE
# endif
BDDM_EXTERN
int llh_m0t( double *x, double *y, double *u,
double *theta, double *ptheta, int ny, int nt, double *llh);
...
typedef struct
{
double alpha;
double gamma;
double tau;
} ThetaDCM;
# endif
顶部的指令用于检查编译器是否为 nvcc(cuda 代码的编译器)。现在我意识到有一个错误,我应该这样做:
# ifndef DDM_HEADER
# define DDM_HEADER
# ifdef __CUDACC__
#include "math.h"
#include "curand_kernel.h"
#include "curand.h"
#include <stdio.h>
...
# endif
BDDM_EXTERN
int llh_m0t( double *x, double *y, double *u,
double *theta, double *ptheta, int ny, int nt, double *llh);
...
typedef struct
{
double alpha;
double gamma;
double tau;
} ThetaDCM;
# endif
让我困惑的是cython尽管有 # ifdef 编译的代码CUDACC。我使用 cython 来包装第一个 #ifdef 语句中定义的 c 函数(如 llh_m0t),因此令人困惑的是 cython 可以看到这些函数定义。