c - 为什么在多个编译单元项目中需要将 file.h 文件包含到 file.c 中

标签 c

场景如下:

文件.h

#ifndef _FILE_
#define _FILE_
void foo(void);
#endif

文件.c

#include "file.h"   //why is this line necessary?
void foo(void)
{
   do something here...    
}

最佳答案

这样做是为了捕获头文件中的函数声明与.c 文件中的函数定义之间的不一致。例如,以下将从编译器生成错误消息

文件.h

#ifndef _FILE_
#define _FILE_
void foo(void);
#endif

文件.c

#include "file.h"   //This line is for error checking

void foo( int someArgThatWasntInTheDeclaration )
{
   do something here...    
}

关于c - 为什么在多个编译单元项目中需要将 file.h 文件包含到 file.c 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245258/

相关文章:

c - C 混淆中的结构

c - 回文数 - 编译器既不编译代码也不显示任何错误

c - 当数据变大时在c中搜索二叉树

c - Windows 进程注入(inject)崩溃

c - 接收tcp数据时写入串口

C curses 不捕获鼠标事件

c++ - 如何获取当前执行代码的 HMODULE?

c - 如何通过指针打印数组的元素?

c - C 中的客户端服务器应用程序中缺少字节

C程序: multi-word guessing game conversion error