头文件仅包含在整个程序一次? [英] Header file included only once in entire program?

查看:116
本文介绍了头文件仅包含在整个程序一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个常见的​​问题,但我仍然不能完全得到我的头周围。

I know this is a common question but I still can't fully get my head around it.

C 或C ++从多个不同的源文件和头文件生成的程序,将每个头文件只能在当使用头警卫整个code包含一次?

In a C or C++ program generated from multiple different source and header files, will each header file be only included once in the entire code when the header guards are used?

有人告诉我,previously一个头文件(包括警卫)将获得包括只在一个翻译单元,但在整个code多次一次。这是真的吗?

Someone told me previously that a header file (with include guards) will get included only once in one translation unit but multiple times in the entire code. Is this true?

如果它得到仅包括在整个code,当一个文件希望包括它和preprocessor检测到它已经被列入,请问该文件希望使用它知道下落一次在code这是previously包括在内?

If it gets included only once throughout the entire code, when one file wishes to include it and the preprocessor detects that it has already been included, how does that file that wishes to use it know whereabouts in the code it was previously included ?

推荐答案

这是一个过程:

source           header   source header header
   \           /        \   |      /   /
    \         /          \  |     /   /
  PREPROCESSOR            PREPROCESSOR
       |                      |
       V                      V
 preprocessed code      preprocessed code
       |                      |
    COMPILER               COMPILER
       |                      |
       V                      V
  object code              object code
             \            /
              \          /
               \        /
                 LINKER
                   | 
                   V
               executable

preprocessing

的#include 是第一步。它指示preprocessor到进程指定的文件,然后将结果插入到输出

#include is for this first step. It instructs the preprocessor to processes the specified file, and insert the result into the output.

如果 A 包括 B C ,和 B 包括 C 中,preprocessor的输出 A 将包括对处理的文本 C 两次。

If A includes B and C, and B includes C, the preprocessor's output for A will include the processed text of C twice.

这是一个问题,因为它会导致重复的声明。一个补救方法是使用preprocessor变量跟踪源$ C ​​$ C是否已被列入(又名头卫士)。

This is a problem, since it will result in duplicate declarations. A remedy is to use preprocessor variables track whether the source code has been included (aka header guards).

#ifndef EXAMPLE_H
#define EXAMPLE_H

// header contents

#endif

第一次, EXAMPLE_H 是不确定的,而preprocessor将评估范围内的内容 IFNDEF / ENDIF 块。第二次,它会跳过该块。因此,处理输出的修改的,并且定义包含一次。

The first time, EXAMPLE_H is undefined, and the preprocessor will evaluate the contents within the ifndef/endif block. The second time, it will skip that block. So the processed output changes, and the definitions are included only once.

这是很常见的,存在由一些编译器是短并且不需要选择一个独特preprocessor变量实施非标准指令:

This is so common that there is a non-standard directive implemented by some compilers that is shorter and does not require choosing a unique preprocessor variable:

#pragma once

// header contents

您可以找出你想要怎样的便携式你的C / C ++ code,且该头文件保护使用。

You can figure out how portable you want your C/C++ code, and which header guard to use.

页眉警卫将确保每个头文件的内容是present最多一次在preprocessed code的翻译单元。

Headers guards will ensure the contents of each header file are present at most once in the preprocessed code for a translation unit.

编译

编译器从$ P $生成机code pprocessed C / C ++。

The compiler generates machine code from your preprocessed C/C++.

通常,头文件只包括声明,而不是实际的定义(又名实现)。编译器包括一个符号表,任何当前缺少一个定义。

Generally, the header files only include declarations, not the actual definitions (aka implementations). The compiler includes a symbol table for anything that is currently missing an definition.

链接

该链接器将目标文件。它的定义(又名实现)与引用符号表相匹配

The linker combines the object files. It matches up the definitions (aka implementations) with the references to the symbol table.

这可能是两个目标文件提供的定义,链接器将采取之一。出现这种情况,如果你已经把可执行code在你的头。这一般不会用C发生,但它发生用C非常频繁++,因为模板。

It may be that two object files provide the definition, and the linker will take one. This happens if you've put executable code in your headers. This generally does not happen in C, but it happens very frequently in C++, because of templates.

头code,无论声明或定义,包括多次对所有对象文件但链接器合并所有的在一起,所以,只有present一次在可执行文件。(我不包括内联函数,这是present多次。)

The header "code", whether declarations or definitions, is included multiple times across all object files but the linker merges all of that together, so that it is only present once in the executable. (I'm excluding inlined functions, which are present multiple times.)

这篇关于头文件仅包含在整个程序一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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