stdio包含在头文件中

vde*_*nne 2 c stdio include header-files

我不完全确定,但这看起来不对劲:

我有一个名为fraction.h的头文件,其中我存储了一个分数结构和处理它的方法,一个方法用于在文件中写一个分数,在这个函数的签名中,一个参数是一个FILE指针.

fraction.h:

...
const Fraction * fraction_fwrite(const Fraction * f, FILE * file);
...
Run Code Online (Sandbox Code Playgroud)

fraction.c:

#include <stdio.h>
#include "fraction.h"
...
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试编译使用分数的程序时,我收到错误,

这是我在Makefile中的内容:

program_test: fraction.o program_test.o
Run Code Online (Sandbox Code Playgroud)

我当然在program_test.c中包含了fraction.h.但我一直收到这个错误:

fraction.h:34:54:错误:未知类型名称'FILE'

有人可以解释编译器包含文件的不同步骤吗?因为<stdio.h>是在fraction.c中,为什么它会打击这个不明显的类型错误?

我应该包含<stdio.h>fraction.h中吗?从我的经验来看,这看起来并不合适.

Bo *_*son 5

编译时program_test,编译器不会查看其他.c文件,只#include查看实际编译的文件中的文件.

因此,您必须#include <stdio.h>在测试程序中,就像在fraction.c,或将其包含在头文件中.

尽管C标准表明标准库文件不会相互包含,但没有任何迹象表明用户定义的文件不能这样做.事实上,如果他们这样做,通常会更容易使用它们.