The commonly used definition of a translation unit is what comes after preprocessing (header files inclusions, macros, etc along with the source file). This definition is reasonably clear and the C standard, 5.1.1.1, C11, says:
A C program need not all be translated at the same time. The text of the program is kept in units called source files, (or preprocessing files) in this International Standard. A source file together with all the headers and source files included via the preprocessing directive #include is known as a preprocessing translation unit. After preprocessing, a preprocessing translation unit is called a translation unit.
Reading the first sentence more closely:
A C program need not all be translated at the same time.
which implies (to my reading), a C program can be translated at the same without necessarily splitting them into multiple preprocessing source files.
Also at the end of the same paragraph, the standard says:
Translation units may be separately translated and then later linked to produce an executable program.
which can be (and typically is) interpreted as compiling individual object files and then finally linking them to produce a single executable program. However, if one can make a question out of the above statement and ask: does it mean an implementation is free to consider multiple source files as a single translation unit, especially for an invocation like:
gcc file1.c file2.c -o out
where the compiler has access to the entire source?
In particular, if an implementation treats file1.c + file2.c (above) as a single translation unit, can it be considered non-conforming?
解决方案
In the second line you quoted:
The text of the program is kept in units called source files, (or preprocessing files) in this International Standard
If there are two source files then there are two preprocessing files, and therefore two preprocessing translation units, and therefore two translation units. One corresponding to each source file.
The standard doesn't define source file. I guess the compiler could say "I'm making up my own version of 'source file' by declaring that file1.c and file2.c are not source files after all!" and concatenate them, but this would be at odds with programmer expectations. I think you would have a hard time arguing that file1.c is not a source file.
C标准指出程序可以不同时翻译,源文件与包含的头文件一起构成预处理翻译单元,预处理后成为翻译单元。一个程序可以将翻译单元分别翻译再链接成可执行程序。然而,对于`gcc file1.c file2.c -o out`这样的命令,虽然编译器可以访问所有资源,但实现中不能将多个源文件视为单一翻译单元,否则可能不符合标准。每个源文件对应一个独立的翻译单元,因此在上述例子中,file1.c 和 file2.c 应被视为两个不同的翻译单元。

被折叠的 条评论
为什么被折叠?



