What's the difference between the terms “source file” and “translation unit”?

ⅰ亾dé卋堺 提交于 2019-11-27 21:08:18

问题


What's the difference between source file and translation unit?


回答1:


From the C++ Standard:

A source file together with all the headers and source files included via the preprocessing directive #include less any source line skipped by any of the conditional inclusion preprocessing directives is called a translation unit.




回答2:


A "translation unit" is a source file plus any headers or other source files it #includes, plus any files that THEY include, and so on. A source file is just that...one source file.

If it helps any, think of the source file as the "before" the preprocessor runs, and the translation unit as "after". Or, think of it as the preprocessor's job to turn a source file into a translation unit.




回答3:


A translation unit is the basic unit of compilation in C++. It contains:

  • all the contents of a single source file after the preprocessor has run its course
  • the contents of any header files directly or indirectly included by it
  • minus any lines ignored using conditional preprocessing statements

A single translation unit gets compiled into an object file, library, or executable program.

A source file, by contrast, is a stand-alone file, just like any other file on your file system. Once compiled, it can be a component of a translation unit as mentioned above.



来源:https://stackoverflow.com/questions/2795443/whats-the-difference-between-the-terms-source-file-and-translation-unit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!