C头后卫 [英] C include guard

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

问题描述

file1.c中包括 inc.h (含包括后卫的#ifndef INC_H )的第一次,执行的#define INC_H 。但是现在,当另一个 file2.c中包含相同的 inc.h ,是宏 INC_H 已定义的,它所有的同样的故事和previous定义不在这里繁殖?


解决方案

  

但现在,当另一file2.c中包括相同inc.h,已定义的宏INC_H,


是和否这要看情况。


  • 如果 file2.c中包括一些头,其中包括 inc.h INC_H 已经为 file2.c中定义。这是列入任何级别如此。


  • 否则,尚未确定。


护罩$ P从​​被包括在一个文件中,直接或间接地$ pvent头,不止一次!

When file1.c includes inc.h (containing the include guard #ifndef INC_H) for the first time, the #define INC_H is performed. But now, when another file2.c includes the same inc.h, is the macro INC_H already defined, all it's the same story and previous definition is not propagated here?

解决方案

But now, when another file2.c includes the same inc.h, is the macro INC_H already defined,

Yes and No. It depends.

  • If file2.c includes some header which includes inc.h, then yes, INC_H is already defined for file2.c. This is true for any level of inclusion.

  • Else, no it is not already defined.

Guards prevent header from being included in a file, indirectly or directly, more than once!

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

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