CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2003
    Posts
    13

    Translation Unit

    Hello, All:

    What is C++ translation unit? Is it talking about file *.h/*.cpp? Why is that?

    Thanks.

    csheng

  2. #2
    Join Date
    Nov 2003
    Location
    Pasadena, CA
    Posts
    48
    I'm sure a google search or glance through the standard will provide a well-worded definition - but the big picture is all of the code seen by the compiler and compiled into an object file.

    so if you've got:

    main.c

    #include<iostream>
    #include<cstdlib>

    int main(void) {return 0;}

    EOF

    then the translation unit is the contents of iostream.h plus the contents of all of the files it includes (and so on recursivly)

    dido on cstdlib

    and finally the function main.

    For most (command line) compilers if you give it multiple .c files they are combined into a single translation unit.
    The views expressed are those of the author and do not reflect any position taken by the Goverment of the United States of America, National Aeronautics and Space Administration (NASA), Jet Propulsion Laboratory (JPL), or California Institute of Technology (CalTech)

  3. #3
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    A translation unit is usually a *.c or *.cpp file.

    Most compilers do NOT compile multiple *.c/*.cpp fies into a single translation unit. They remain seperate (even with command line compilers).
    FYI: Most compilers are command line compilers.

    A *.c file can be combine into another *.c file as a single translation unit IF, the second *.c file includes the other.

    #include <foo.c>

    Otherwise for the most part each *.c file is a single translation unit.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  4. #4
    Join Date
    Nov 2003
    Location
    Pasadena, CA
    Posts
    48
    Originally posted by Axter
    Most compilers do NOT compile multiple *.c/*.cpp fies into a single translation unit. They remain seperate (even with command line compilers).
    Out of curiosity; is this dictated by the standard or simply compiler convention. I know the standard does provide a definition of translation unit but its not the easist doc to find stuff in quickly.
    The views expressed are those of the author and do not reflect any position taken by the Goverment of the United States of America, National Aeronautics and Space Administration (NASA), Jet Propulsion Laboratory (JPL), or California Institute of Technology (CalTech)

  5. #5
    Join Date
    Nov 2003
    Posts
    13
    It appears to be clear that .c file is compiled to a translation, but if the a.c file has

    #include "a.h"

    Compiler will compile these two file to a translation unit.

    However, a a.h file can be compiled to a single translation unit as well.

    In this case, what is the use of the translation unit? is it being used to determine variable/object scope?

    I was initially curious to know how translation unit is generated, and more importantly, how it is used?

    Regards,
    csheng

  6. #6
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Originally posted by csheng
    However, a a.h file can be compiled to a single translation unit as well.
    You have a compiler that compiles .h files?
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  7. #7
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    well since we are in the VC++ forum, I'll just add there is documentation in msdn look for 'translation phazes" "translation units"

  8. #8
    Join Date
    Nov 2003
    Posts
    13
    Let me re-phrase the question.

    If a.h file is included in both a.c and b.c file, both c file are to be compiled into two translation units.

    In this case, what is the use of the translation unit? is it being used to determine variable/object scope?

    I was initially curious to know how translation unit is generated, and more importantly, how it is used?

    Maybe it is a stupid question, who cares?

    csheng


  9. #9
    Join Date
    Nov 2003
    Posts
    13
    Thanks to mclark for pointing to searching msdn. It did expalin something about translation phase.

    But I still could not find what role translation unit is playing in MSDN. This is a C question, but it is also a c++ question as well.



    csheng

  10. #10
    Join Date
    Nov 2003
    Posts
    13
    Ooops, I found it in MSDN, thanks very much.

    Here is from MSDN for those of you who do not want to search MSDN. (Actually I use MSDN very often).

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

    Source files can be translated at different times — in fact, it is common to translate only out-of-date files. The translated translation units can be kept either in separate object files or in object-code libraries. These separate translation units are then linked to form an executable program or a dynamic-link library (DLL).

    Translation units can communicate using:

    Calls to functions that have external linkage.
    Calls to class member functions that have external linkage.
    Direct modification of objects that have external linkage.
    Direct modification of files.
    Interprocess communication (for Microsoft® Windows®-based applications only).


    Thanks to mclark again.

    csheng


  11. #11
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Thanks to mclark again.
    Actually, it would appear that Mick was the first to suggest searching MSDN...

    Though I would almost expect that one would hit MSDN before CG... (but from experience I am quite aware that that is not the case).
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured