Seperate compilation (header files n' their implementations)

Discussion in 'C++' started by Chris Mantoulidis, Dec 19, 2003.

  1. Seperate compilation (that's what it's called, right?) seems to be
    quite popular, so I decided to get some info about it, and (d'oh) use
    it...

    But it's whole structure seems weird to me...

    Here's what I think of how it is (from what I've read):

    THE PROJECT
    +1st header file
    +Implementation of the 1st header file (includes the header file)
    +2nd header file
    +Implementation of the 2nd header file (includes the header file)
    ........
    +Nth header file
    +Implementation of the Nth header file (includes the header file)
    +MAIN program which includes the header files

    I guess the above structure is correct...

    But here's what I don't understand...

    Since the main program includes the header files, it only has the
    interfaces and not the implementations of the headers (since the
    implementations are in the .cc files, and not in the .h files).

    But the headers DON'T include their implementations; the opposite
    though does happen...

    So how can the program be aware of the implementations since it seems
    like the .cc files are nowhere included??????

    And how would I compile such a project? For example, let's say I
    compile the main program... Whatever will happen of the
    implementations???

    Thanks in advance and for your patience,
    cmad
    Chris Mantoulidis, Dec 19, 2003
    #1
    1. Advertising

  2. "Chris Mantoulidis" <> wrote...
    > Seperate compilation (that's what it's called, right?)


    I always thought it was spelled "sepArate compilation"...

    > seems to be
    > quite popular, so I decided to get some info about it, and (d'oh) use
    > it...


    What's the "d'oh" about? Did you mean "duh" instead of "d'oh"?

    >
    > But it's whole structure seems weird to me...
    >
    > Here's what I think of how it is (from what I've read):
    >
    > THE PROJECT
    > +1st header file
    > +Implementation of the 1st header file (includes the header file)


    The correct way of saying would be:

    1st header: definition of the 1st class and related declarations
    1st translation unit: definitions of 1st class members and other
    related objects/functions.
    etc.

    > +2nd header file
    > +Implementation of the 2nd header file (includes the header file)
    > .......
    > +Nth header file
    > +Implementation of the Nth header file (includes the header file)
    > +MAIN program which includes the header files
    >
    > I guess the above structure is correct...


    Depends on how you look at it.

    >
    > But here's what I don't understand...
    >
    > Since the main program includes the header files, it only has the
    > interfaces and not the implementations of the headers (since the
    > implementations are in the .cc files, and not in the .h files).


    That's basically what it boils down to, yes.

    >
    > But the headers DON'T include their implementations; the opposite
    > though does happen...


    Headers _contain_ declarations and class definitions. They don't
    include anything.

    >
    > So how can the program be aware of the implementations since it seems
    > like the .cc files are nowhere included??????


    It's _linked_ at the _linking_ stage of building the program.

    >
    > And how would I compile such a project?


    Just like you said in te beginning, _separately_.

    > For example, let's say I
    > compile the main program... Whatever will happen of the
    > implementations???


    You compile them too.

    I guess you need a decent book that describes how to use your
    compiler/linker/librarian programs. It very much depends on what
    compiler/platform you're using. And it pretty much goes beyond the
    scope of this newsgroup. I recommend asking in a newsgroup for
    your compiler.

    Victor
    Victor Bazarov, Dec 19, 2003
    #2
    1. Advertising

  3. Chris Mantoulidis

    osmium Guest

    Chris Mantoulidis writes:

    > So how can the program be aware of the implementations since it seems
    > like the .cc files are nowhere included??????


    The linker knows where the libraries are, it found out by some magic when
    the compiler was installed/whatever.
    osmium, Dec 19, 2003
    #3
  4. (Chris Mantoulidis) wrote in message
    > Since the main program includes the header files, it only has the
    > interfaces and not the implementations of the headers (since the
    > implementations are in the .cc files, and not in the .h files).
    >
    > But the headers DON'T include their implementations; the opposite
    > though does happen...
    >
    > So how can the program be aware of the implementations since it seems
    > like the .cc files are nowhere included??????
    >
    > And how would I compile such a project? For example, let's say I
    > compile the main program... Whatever will happen of the
    > implementations???
    >
    > Thanks in advance and for your patience,
    > cmad



    It sounds like you lack a real understanding of how a binary is
    produced from source code. There are two steps, compilation and
    linking.

    In compilation, the code is validated by the compiler and translated
    into blocks of machine code called object files. The compiler does
    not care how the functions in other blocks are implemented. Header
    files are the interface between the blocks and the compiler simply
    validates how you call a function against how this function is defined
    in the header.

    The compiler will only complain if it finds a function call and no
    matching declaration for this function--for good reason. A mismatch
    may indicate a typo or other syntatical error in your code.

    Once an object file exists for each block of code, a single executable
    is produced. It is at this point where all of the function calls and
    implementations are connected--linked--together. If you have a
    function that is defined and not used, *and* if you call that function
    somewhere in the code it will not be noticed until this step.

    There are TONS of resources available to read up on this. Try google.

    ---
    Jared Dykstra
    http://www.bork.org/~jared
    Jared Dykstra, Dec 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SenderX
    Replies:
    7
    Views:
    373
    Greg Comeau
    Aug 29, 2003
  2. Andreas Schmidt
    Replies:
    2
    Views:
    300
    David Harmon
    Apr 9, 2004
  3. moondaddy
    Replies:
    2
    Views:
    578
    Steven Cheng[MSFT]
    Oct 16, 2006
  4. C__chp
    Replies:
    4
    Views:
    503
    Puppet_Sock
    Feb 15, 2008
  5. Andrew Connell
    Replies:
    0
    Views:
    106
    Andrew Connell
    Nov 19, 2003
Loading...

Share This Page