【发布时间】:2016-12-24 16:35:43
【问题描述】:
我有以下项目结构:
文件 - a.h
#pragma once
struct best_fit_struct {
void *next;
size_t size;
};
文件 - b.h
#pragma once
typedef struct mm_t {
int type;
union {
struct best_fit_struct best_fit_mm;
} per_mm_struct;
void *memory;
} mm_t;
文件 - b.c
#include "a.h"
#include "b.h"
在使用gcc -c b.c 编译b.c 时,会抛出以下错误
file best_fit_mm has incomplete data type
我在b.h 之前添加了a.h,所以排序对我来说很合适。
令人惊讶的是,如果我在b.h 中包含a.h,事情就会得到解决。
【问题讨论】:
-
"所以我不想将 a.h 包含在其中。" - 由于您遇到的错误,不会发生。如果没有
struct a的定义,编译器如何知道union的大小? -
@FaizHalde:不,除非你
#include "a.h"。你不能只排除这一点。这是编译器如何工作的规则。无论如何,它无法在没有struct a的定义的情况下编译。 -
“令人惊讶的是,如果我在 b.h 中包含 a.h,事情就会得到解决。”
-
@FaizHalde,因为它对我有用,所以有些事情你可能没有告诉我们。您是否使用 gcc 的
-E标志仅运行预处理器并查看结果以确保包含按预期工作?我敢打赌,在a.h之前还有其他内容包括b.h。 -
对不起,伙计们。确实是缺少导致问题的头文件。解决了。谢谢!我应该发布只包含 b.h 文件的 main.c 文件
标签: c header-files