python - 避免在 cython 中对链表进行嵌套结构重定义
问题描述
我想将与链表结构相关的代码移动到单独的文件中。此链表用于 .pyx 和 .c 文件。
当前实现:cython_file.pyx:
ctypedef struct linked_list
ctypedef struct linked_list:
double price
double volume
int data
linked_list *next_cell
...
c_file.c:
typedef struct linked_list {
double price;
double volume;
int data;
struct linked_list * next_cell;
} linked_list;
...
我想要的是创建LinkedList.h
,LinkedList.c
和LinkedList.pxd
, 它将包含以下内容:
链表.h:
typedef struct linked_list {
double price;
double volume;
int data;
struct linked_list * next_cell;
} linked_list;
...
链表.c:
#include "LinkedList.h"
...
链表.pxd:
cdef extern from "LinkedList.h":
ctypedef struct linked_list
ctypedef struct linked_list:
double price
double volume
int data
linked_list * next_cell
我想以下列方式使用它:在 cython_file.pyx 中:
from LinkedList cimport *
...
在 c_file.c 中:
#include "LinkedList.h"
...
当我尝试编译第二个变体时,出现错误:
LinkedList.h(1): error C2011: 'linked_list': 'struct' type redefinition
我想这个问题是由于嵌套结构而发生的
解决方案
为避免redefinition errors
您必须在头文件中使用包含保护。
//LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
...
#endif
包含保护是预处理器宏,可防止多次包含头文件。
它基本上检查是否LINKEDLIST_H
已定义。如果是这样,它会跳过if
子句中的所有内容。否则它定义它。
这样您就可以避免多次从标题中重新定义结构,这可能是导致此错误的原因。
推荐阅读
- mysql - mysqlpump 如何忽略数据库的名称?
- assembly - 如何在宏中指定寄存器大小 (b/w/d)
- elasticsearch - 当我在 Elastic Search 中重新索引时,我无法使用包含破折号的新名称重命名字段
- python - Python 异常 ImportError - 尝试使用不知道父包的相对导入
- python - 如何使用 Python 脚本将 HTML 批量转换为 Markdown
- azure - Azure 数据工厂 - 设置 blob 容器的元数据以及“复制”活动
- amazon-web-services - 如何将 PATH 导出到“sam build”命令?
- flutter - 是否有任何 SDK 在 Flutter 中我们可以在同一屏幕上看到七个直播流媒体?
- hibernate - Hibernate:添加强制谓词
- javascript - API 仅在我开始输入后呈现(Firebase)