【发布时间】:2021-05-18 14:37:07
【问题描述】:
对于上下文,我正在编写一个操作系统:
我有一个struct vt_device_s 和一个struct __vt_device_s,它们是特定于架构的,并且存在于vt_device_s 中,如下所示:
struct
vt_device_s
{
struct __vt_device_s __device;
size_t cursor_x;
size_t cursor_y;
};
现在是架构结构:
struct
__vt_device_s
{
uint16_t *memory;
size_t memory_len;
};
标头<dev/vt.h> 知道__vt_device_s 在<sys/_vt.h> 中定义,因为它已包含在内,但我收到此错误:
error: field '__device' has incomplete type
48 | struct __vt_device_s __device;
|
我意识到这是因为两个文件相互依赖(整个冲突是由_vt.c 包括_vt.h 包括vt.h 包括_vt.h 引起的)但我不明白这是一个编译问题。我在两个文件中都包含了警卫!
PS:我知道如果我使用指针,这将不是问题,但由于它是一个操作系统,因此该驱动程序需要在设置分页之前运行(即,malloc 和 free 不要还存在)。
以下是有问题的三个文件:
dev/vt.h
#ifndef _DEV_VT_H_
#define _DEV_VT_H_ 1
#include <stddef.h>
#include <sys/_vt.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct
vt_device_s
{
struct __vt_device_s __device;
size_t cursor_x;
size_t cursor_y;
};
void vt_init(struct vt_device_s *);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _DEV_VT_H_ */
sys/_vt.h
#ifndef _I386__VT_H_
#define _I386__VT_H_ 1
#include <stddef.h>
#include <stdint.h>
#include <dev/vt.h>
#define __VT_WIDTH 80
#define __VT_HEIGHT 25
#define __VT_MEMOFF 0xb8000
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct
__vt_device_s
{
uint16_t *memory;
size_t memory_len;
};
void __vt_init(struct vt_device_s *);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _I386__VT_H_ */
sys/_vt.c
#include <sys/_vt.h>
void
__vt_init(struct vt_device_s *device)
{
device->__device.memory = (uint16_t *) __VT_MEMOFF;
device->__device.memory_len = __VT_WIDTH * __VT_HEIGHT;
}
【问题讨论】:
标签: c struct compiler-errors dependencies circular-dependency