我当时正在读Scott Meyers写的“Effective c++” 偶然发现了“翻译单元”这个术语。
谁能给我解释一下:
到底是什么? 当我用c++编程时,什么时候应该考虑使用它? 它只与c++相关,还是也可以与其他编程语言一起使用?
我可能已经在不知道术语的情况下使用它了……
我当时正在读Scott Meyers写的“Effective c++” 偶然发现了“翻译单元”这个术语。
谁能给我解释一下:
到底是什么? 当我用c++编程时,什么时候应该考虑使用它? 它只与c++相关,还是也可以与其他编程语言一起使用?
我可能已经在不知道术语的情况下使用它了……
当前回答
翻译单元是一个文件(.c/.cpp),在它包含了所有的头文件之后。
http://web.archive.org/web/20091213073754/http: / / msdn.microsoft.com/en-us/library/bxss3ska (VS.80) . aspx
其他回答
正如其他人所说,翻译单元基本上是源文件经过预处理后的内容。它是语言语法的最高产物;只有在编写C或c++编译器时才需要担心这个问题。
这本书讲得够清楚了。当Meyers提到“翻译单元”时,他指的是源代码文件。
在我看来,“翻译单元”通常是一个“后预处理”源文件。您可以在这个MSDN页面上获得更多详细信息。http://msdn.microsoft.com/en-us/library/bxss3ska (v = vs.80) . aspx
从这里:(时光倒流机链接)
According to standard C++ (wayback machine link) : A translation unit is the basic unit of compilation in C++. It consists of the contents of a single source file, plus the contents of any header files directly or indirectly included by it, minus those lines that were ignored using conditional preprocessing statements. A single translation unit can be compiled into an object file, library, or executable program. The notion of a translation unit is most often mentioned in the contexts of the One Definition Rule, and templates.
每个cpp/c(实现)文件将被转换为一个翻译单元(即。CPP文件中的object file (.obj)头文件将被替换为头文件中的实际文本。