【发布时间】:2018-12-06 18:15:42
【问题描述】:
我正在从事一个项目,我必须遵循文件组织指南,但无法编译。
为了简化它,我有一个 main.h,我必须在其中定义 bool 和一些符号:
#ifndef main_h
#define main_h
#include <stdio.h>
#include "test.h"
typedef unsigned char bool;
#define TRUE 1
#define FALSE 0
#endif /* main_h */
然后 main.c 必须使用类型“num_seconds_t”和函数“test()”,这两者都必须位于与 main 不同的文件中。
所以我有我的 test.h:
#ifndef test_h
#define test_h
#include <stdio.h>
#include "main.h"
typedef int32_t num_seconds_t;
bool test(num_seconds_t var);
#endif /* test_h */
还有我的 test.c:
#include "test.h"
bool test(num_seconds_t var){
num_seconds_t test = var;
return TRUE;
}
我认为 main.c 不会对这个问题产生任何影响。
错误在我的 test.h 文件中声明了未知类型“bool”,我有点理解为什么当它在 main.h 中点击“test.h”的包含时,它会在它之前开始遍历该文件在 main.h 中定义了 bool,然后由于 main.h 在 test.h 中点击“#include”main.h”时具有“#ifndef main_h”,因此它会跳过此并继续读取,因此 bool 直到测试之后才被定义.h 已完成读取。
我不确定我的理解是否正确,但解决此问题的正确方法是什么。通过简单地将“#include”test.h“”移动到 bool 的定义之后,它将编译,但在我的大项目中,我有许多文件交织在一起,并且协调包含这些文件的顺序将非常困难,如果不是不可能的话。
谢谢
【问题讨论】:
-
为什么你的主标题中有
#include "test.h"?那里似乎没有必要 -
将你的 typedef 移动到第三个文件中,并将其包含在 test 和 main 中。
-
在这种情况下,您应该在 main.c 文件中包含 test.h,而不是标题 - 仅在您实际需要的地方包含标题可以解决很多问题
-
@VladRusu 它是项目的一部分,我不允许在其他任何地方定义类型。我同意这样做会更合乎逻辑
-
旁白:为什么要创建
typedef unsigned char bool;而不是使用C 布尔类型_Bool或bool via #include`?
标签: c