如何在c ++中定义编译单元? [英] How is compilation unit defined in c++?

查看:114
本文介绍了如何在c ++中定义编译单元?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么是翻译单位? in C ++


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

解决方案

头文件没有独立的生命,只有内容 #included 转换为.c或.cpp文件。但由于 #include 由预处理器处理,编译器不知道有关不同的头文件;它只看到生成的代码列表作为输入。这是所谓的编译单元:具有所有 #include 指令的源文件被相关头文件的内容替换。 p>

Possible Duplicate:
What is a “translation unit” in C++

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 ?

解决方案

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.

这篇关于如何在c ++中定义编译单元?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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