简体   繁体   English

C中的标题保护和重新分配问题

[英]Header guards and redefenitions issues in c

I have a project with 3 files and I am quite confused as to what's the proper include order and what header guards are needed to avoid redefinition issues. 我有一个包含3个文件的项目,我对正确的包含顺序以及避免重新定义问题所需的标题保护感到困惑。 Lets call the files ac, ah, bcbh, cc, ch There are functions I need in all 3 of the files so I am not sure where to put them in a way it won't cause redefinition. 让我们将文件称为ac,ah,bcbh,cc,ch。在所有这三个文件中都需要一些功能,因此我不确定将其放置在什么位置,不会引起重新定义。 ah is the main file - and it has includes to bh and chbh and ch don't include one another. ah是主文件-并包含bh和chbh,而ch不包含另一个文件。 How to solve this? 如何解决呢? Where do I add header guards and do I add them on the entire header file or only on the section in the header files that exists in other header files that include this one? 我在哪里添加标头防护,并且将它们添加到整个标头文件中还是仅添加到包含该标头的其他标头文件中的标头文件中的节上? (I mean - I think I mustn't add header guards on the unique parts of the header file - that don't exist in the pother header files that include it). (我的意思是-我想我不应该在头文件的唯一部分上添加头保护-在包含它的pother头文件中不存在)。

look up include guards it goes like 查找包括警卫队

#ifndef A.H
#define A.H

"your code"

#endif

the name you choose for your guard does not matter. 您为警卫队选择的名称并不重要。

alternative you can just use #prama once 或者,您可以只使用一次#prama

on top of your program it is widely supported. 在您的程序之上,它得到了广泛的支持。

I would say you should try to understand the hierarch y of included headers if you can. 我想说的是,如果可以的话,您应该尝试理解包含标头的层次结构。 confusion is bad 混乱是坏的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM