为什么包括警卫? [英] Why include guards?

查看:149
本文介绍了为什么包括警卫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学C。

包括警卫,定义这里,用于prevent加载相同code两倍编译。

Include guards, as defined here, are used to prevent loading the same code twice at compilation.

为什么我的编译器(GCC)检测到它加载在同一code两次,并有合理的默认行为?

Why can't my compiler (GCC) detect that it is loading the same code twice and have a sensible default behaviour?

推荐答案

很简单,因为你可能会想编译器加载该文件的两倍。

Simply because you might have wanted the compiler to load that file twice.

记住,的#include 只加载一个文件,并把它的内容在指令的地方。该文件可能是一个标题文件中,但也可以是源$ C ​​$的c有用和经常使用的片,以及

Remember, that #include simply loads a file and puts its contents in the place of the directive. This file might be a header file, but may be useful and frequently used piece of source code as well.

大多数现代编译反应一度的#pragma 做你希望他们到底是什么。但请记住,这是不包括在语言规范的编译器扩展,它通常是一个好主意,坚持包括警卫 - 你会肯定的,那它适用于所有的编译器和在任何情况下

Most modern compilers react to #pragma once doing exactly what you want them to. Remember though, that this is a compiler extension not included in the language specification and it is generally a good idea to stick to include guards - you'll be certain, that it works on every compiler and in any circumstances.

这篇关于为什么包括警卫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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