Why include guards?(为什么要加入守卫?)
问题描述
包含守卫,如此处所定义,用于防止两次加载相同的代码编译.
Include guards, as defined here, are used to prevent loading the same code twice at compilation.
为什么我的编译器 (GCC) 无法检测到它正在加载相同的代码两次并且具有合理的默认行为?
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
只是加载一个文件并将其内容放在指令的位置.该文件可能是头文件,但也可能是有用且经常使用的源代码.
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 once
做出反应,完全按照您的意愿行事.但请记住,这是一个未包含在语言规范中的编译器扩展,坚持包含保护通常是个好主意 - 您会确定它适用于所有编译器和任何情况.
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.
这篇关于为什么要加入守卫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么要加入守卫?


基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01