每个源文件的标头 [英] Header per source file

查看:21
本文介绍了每个源文件的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解每个源文件方法都有一个标头背后的目的.正如我所看到的,标头用于在使用它们的几个文件之间共享函数声明、typedef 和宏.当您为 .c 文件制作头文件时,它的缺点是每次您想要查看函数声明或宏时都需要参考头文件,而且通常所有内容都更简单位于一个源文件中(当然不是整个软件).

I'm trying to understand the purpose behind one header per each source file method. As I see it, headers are meant for sharing function declarations, typedef's and macro's between several files that utilize them. When you make a header file for your .c file it has the disadvantage that each time you want to see a function declaration or macro you need to refer to the header file, and generally it is simpler that everything is in one source file (not the whole software, of course).

那么程序员为什么要使用这种方法呢?

So why do programmers use this method?

推荐答案

C 中的头文件将声明(必须对使用函数的每个 .c 文件都可用)与定义(必须在一个地方)分开.此外,它们提供了一点模块化,因为您只能将公共接口放入头文件中,而不提及应该在 .c 文件内部的函数和静态变量.它使用文件系统来提供公共接口和私有实现.

The header files in C separate declarations (which must be available to each .c file that uses the functions) from the definitions (which must be in one place). Further, they provide a little modularity, since you can put only the public interface into a header file, and not mention functions and static variables that should be internal to the .c file. That uses the file system to provide a public interface and private implementation.

一个.h 文件到一个.c 文件的做法主要是为了方便.这样,您就知道声明在 .h 文件中,而定义在相应的 .c 文件中.

The practice of one .h file to one .c file is mostly convenience. That way, you know that the declarations are in the .h file, and the definitions in the corresponding .c file.

这篇关于每个源文件的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆