c++中的编译单元是如何定义的?

2022-01-17 00:00:00 compiler-construction c++

可能重复:
什么是翻译单元"在 C++ 中

常说C/C++中声明的静态变量在编译单元中是不可见的?这是否意味着每个 .c 或 .cpp 文件都是一个单独的编译单元?.h 文件和 .h 文件中声明的静态变量呢?.h 文件是否也被视为单独的编译单元?

It is often said that the static variables declared in C/C++ are not visible across compilation units ? Does this mean that each .c or .cpp file is a seperate compilation unit ? What about a ,h file and the static variables declared in the .h file ? Is .h file also considered as a separate compilation unit ?

推荐答案

头文件没有独立的生命,只有它们的内容是#included 到.c 或.cpp 文件中.但是由于 #include 由预处理器处理,编译器不知道不同的头文件;它只将生成的代码列表视为输入.这就是所谓的编译单元:一个源文件,其所有#include 指令都被相关头文件的内容所取代.

Header files have no separate life, only their content is #included into .c or .cpp files. But since #include is handled by the preprocessor, the compiler has no knowledge about distinct header files; it only sees the resulting code listing as input. This is what is called a compilation unit: a source file with all its #include directives replaced by the content of the relevant header files.

相关文章