C语言中的翻译单位到底是什么

C语言中的翻译单位到底是什么

本文介绍了C语言中的翻译单位到底是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

翻译单元的常用定义是经过预处理(头文件包含,宏等以及源文件)之后的内容.这个定义相当明确,C标准5.1.1.1 C11表示:

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:

更仔细地阅读第一句话:

Reading the first sentence more closely:

(按我的理解),这意味着可以同时翻译C程序 ,而不必将其拆分为多个预处理源文件.同样在同一段落的末尾,该标准说:

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:

可以(通常)解释为编译单个目标文件,然后最终将它们链接以生成单个可执行程序.但是,如果您可以根据以上陈述提出一个问题并问:这是否意味着实现可以自由地将多个源文件视为一个翻译单元,尤其是对于像这样的调用:

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?

特别是,如果实现将file1.c + file2.c(如上)视为单个翻译单元,是否可以认为它不符合要求?

In particular, if an implementation treats file1.c + file2.c (above) as a single translation unit, can it be considered non-conforming?

推荐答案

在第二行中引用:

如果有两个源文件,则有两个预处理文件,因此有两个预处理翻译单元,因此有两个翻译单元.每个源文件对应一个.

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.

该标准未定义源文件.我猜编译器可能会说:我声明file1.cfile2.c毕竟不是源文件,从而构成了自己的'源文件'版本!"并连接它们,但这与程序员的期望不符.我认为您很难争论file1.c不是源文件.

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语言中的翻译单位到底是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 16:57