包含守卫,如此处所定义,用于防止两次加载相同的代码编译.
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.
这篇关于为什么要加入守卫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)