What exactly is a translation unit in C

后端 未结 4 1178
旧巷少年郎
旧巷少年郎 2021-01-03 23:59

The commonly used definition of a translation unit is what comes after preprocessing (header files inclusions, macros, etc along with the source file). This definit

4条回答
  •  青春惊慌失措
    2021-01-04 00:49

    A translation unit means a dot C file. To all intents and purposes, including its associated dot h includes. Rarely #include directives are used to add other file types or other dot C files.

    static variables are visible only within the translation unit. It's very common to have a few public functions with external linkage and many static functions and data items t support. So a C translation unit is a bit like a singleton C++ class. If the compiler doesn't handle static correctly it is non-conforming.

    Typically one object file is created for each translation unit, and they are then linked by the linker. That's not actually mandated by the standard but is the natural and obvious way to do things in an environment where files are cheap to create and compiling is relatively slow.

提交回复
热议问题