什么是 C++ 中的“翻译单元”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1106149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:48:32  来源:igfitidea点击:

What is a "translation unit" in C++

c++

提问by Harry

I am reading at the time the "Effective C++" written by Meyers and came across the term "translation unit".

当时我正在阅读 Meyers 编写的“Effective C++”并遇到了“翻译单元”一词。

Could somebody please give me an explanation of:

有人可以给我一个解释:

1) What exactly it is

1)它到底是什么

2) When should I consider using it when programming with C++

2) 用 C++ 编程时我应该什么时候考虑使用它

3) If it is related only to C++, or it can be used with other programming languages

3)如果只与C++相关,或者可以与其他编程语言一起使用

I might already use it without knowing the term....

我可能已经在不知道这个术语的情况下使用它了......

回答by JeffH

From here: (wayback machine link)

这里:(回程机链接

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.

根据标准 C++回溯机器链接):翻译单元是 C++ 中编译的基本单元。它包含单个源文件的内容,加上它直接或间接包含的任何头文件的内容,减去使用条件预处理语句忽略的那些行。

单个翻译单元可以编译成目标文件、库或可执行程序。

翻译单元的概念在单一定义规则和模板的上下文中最常被提及。

回答by Ana Betts

A translation unit is for all intents and purposes a file (.c/.cpp), afterit's finished including all of the header files.

翻译单元在所有的意图和用途中都是一个文件 (.c/.cpp),它完成包括所有头文件之后。

http://msdn.microsoft.com/en-us/library/bxss3ska%28VS.80%29.aspx

http://msdn.microsoft.com/en-us/library/bxss3ska%28VS.80%29.aspx

回答by Ana Betts

A hard question to answer definitively. The C++ standard states:

一个很难明确回答的问题。C++ 标准规定:

The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.4.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing directives, is called a translation unit. [Note: a C++ program need not all be translated at the same time. ]

程序的文本以本国际标准中称为源文件的单位保存。源文件连同所有头文件 (17.4.1.2) 和通过预处理指令 #include 包含的源文件 (16.2),减去任何条件包含 (16.1) 预处理指令跳过的任何源代码行,称为翻译单元。[注意:C++ 程序不需要同时翻译。]

So for most intents and purposes a translation unit is a single C++ source file and the header or other files it includes via the preprocessor #include mechanism.

因此,对于大多数意图和目的,翻译单元是单个 C++ 源文件及其通过预处理器 #include 机制包含的头文件或其他文件。

Regarding your other questions:

关于您的其他问题:

2) When should I consider using it when programming with C++

2) 用 C++ 编程时我应该什么时候考虑使用它

You can't not consider it - translation units are the basis of a C++ program.

你不能不考虑它 - 翻译单元是 C++ 程序的基础。

3) If it is related only to C++, or it can be used with other programming languages

3)如果只与C++相关,或者可以与其他编程语言一起使用

Other languages have similar concepts, but their semantics will be subtly different. Most other languages don't use a preprocessor, for example.

其他语言也有类似的概念,但它们的语义会略有不同。例如,大多数其他语言不使用预处理器。

回答by Ed S.

The book makes it clear enough. When Meyers referes to a "translation Unit", he means a source code file.

这本书说得够清楚了。当迈耶斯提到“翻译单元”时,他指的是源代码文件。

回答by Allan Stokes

In addition to the ODR, the translation unit is important in the definition of unnamed namespaces, which replaces one of the old uses of "static".

除了 ODR 之外,翻译单元在未命名命名空间的定义中也很重要,它取代了“静态”的旧用法之一。

I guess I still don't have enough points to add a comment under the top answer.

我想我仍然没有足够的分数在最佳答案下添加评论。

回答by sigjuice

A translation unit is code that is passed to the compiler proper. This typically means the output from running the preprocessor on the .c file.

翻译单元是传递给编译器本身的代码。这通常意味着在 .c 文件上运行预处理器的输出。

回答by ganesh marmat

C and C++ programs consist of one or more source files, each of which contains some of the text of the program. A source file, together with its include files (files that are included using the #include preprocessor directive) but not including sections of code removed by conditional-compilation directives such as #if, is called a "translation unit."

C 和 C++ 程序由一个或多个源文件组成,每个源文件都包含程序的一些文本。源文件及其包含文件(使用 #include 预处理器指令包含的文件)但不包括由条件编译指令(如 #if)删除的代码部分,称为“翻译单元”。

回答by rahul

According to MSDN: C and C++ programs consist of one or more source files, each of which contains some of the text of the program. A source file, together with its include files (files that are included using the #include preprocessor directive) but not including sections of code removed by conditional-compilation directives such as #if, is called a "translation unit."

根据MSDN:C 和 C++ 程序由一个或多个源文件组成,每个源文件都包含程序的一些文本。源文件及其包含文件(使用 #include 预处理器指令包含的文件)但不包括由条件编译指令(如 #if)删除的代码部分,称为“翻译单元”。

回答by yesraaj

Every cpp/c (implementation) file will be converted into a translation unit (ie.,object file (.obj)) headers in the cpp file will be replaced with the actual text from the header files.

每个 cpp/c(实现)文件都将被转换为一个翻译单元(即目标文件 (.obj)),cpp 文件中的头文件将被头文件中的实际文本替换。

回答by Yang

In my view, a "translation unit" is typically a single "post-preprocessing" source file. You can get more details on this MSDN page. http://msdn.microsoft.com/en-us/library/bxss3ska(v=vs.80).aspx

在我看来,“翻译单元”通常是一个“后预处理”源文件。您可以在此 MSDN 页面上获得更多详细信息。http://msdn.microsoft.com/en-us/library/bxss3ska(v=vs.80).aspx