What is a “translation unit” in C++

前端 未结 11 689
既然无缘
既然无缘 2020-11-21 07:56

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

Could somebody please give me an explanation of:

相关标签:
11条回答
  • 2020-11-21 08:21

    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.

    0 讨论(0)
  • 2020-11-21 08:28

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

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

    0 讨论(0)
  • 2020-11-21 08:29

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

    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. ]

    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.

    Regarding your other questions:

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

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

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

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

    0 讨论(0)
  • 2020-11-21 08:31

    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."

    0 讨论(0)
  • 2020-11-21 08:32

    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."

    0 讨论(0)
  • 2020-11-21 08:34

    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.

    0 讨论(0)
提交回复
热议问题