Re: #include question

From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/29/04


Date: Sun, 29 Feb 2004 19:42:51 GMT

On Sun, 29 Feb 2004 19:26:59 GMT, "Dan Moos" <dan.moos@verizon.net>
wrote in alt.comp.lang.learn.c-c++:

> hello group.
>
> Okay, this is a very basic question.
>
> Say I have a program who's source spans , oh say , 5 source files
> (translation units as I found them called in Bjarne Stroustrup's book).
>
> Lets say they all use some common header, say <vector>.
>
> So they all #include <vector>
>
> so if I understand it, the code in the header <vector> gets "inserted" into
> 5 different places in the toatal source code (all 5 translation units)
>
> Is this what really happens, or does the linker somehow account for this and
> only #include it once. I always thought that translation units were compiled
> as seperate entities, and before linking, have no knowledge of each other,
> and so I assume that, yes, they all get that code gets included. It just
> seems uneccesary and inefficient to have multiple copies of the same code in
> a project. I mean, in the end the linker makes it one big block of code
> anyway.
>
> I know about multiple inclusion guards, but doesn't that only protect
> against multiple inclusion in the same translation unit? or are they
> specifically for what I am asking about.

What you are missing is what is contained in header files.

Correctly written headers, which includes standard ones and should
include all the ones from third-party libraries and the ones you
create yourself, contain specific things and more importantly omit
specific things.

These are the things that should be contained in headers:

--type definitions (class, struct, template)

--external object declarations (if objects are shared across multiple
source files)

--prototypes for free-standing functions

--macros

And these are the things that should NOT be contained in headers:

--object definitions (create and allocate space for an object)

--function bodies, except for inline functions

The things that do belong in headers do not in themselves generate any
code or create any objects that occupy memory. Even the inline
functions and templates in a header do nothing until a source file
including them instantiates them. Likewise, the type definitions for
classes and structs do nothing until a source file instantiates them
by defining objects of that type, or creating them with new or new[].

Having inline function definitions in headers may cause their bodies
to be inserted in more than one source file, and more than one time in
a source file, but that is what you specifically ask for when you
define them as inline.

So the fact that you include <iostream> in multiple source files, and
different functions in the different source files use the << operator
of std::cout, does not mean that there is more than one cout object in
your program.

Likewise if you include the C header <string.h> or <cstring> and call
the standard function strlen() multiple places, only one copy of the
library function is actually included in or linked to the final
program.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: duplicate functions in multiple modules
    ... I have a project with multiple c files. ... "translation units". ... what we usually call a source file and any headers and anything else ... You need to post a cut-down sample, such as a header file and two ...
    (comp.lang.c)
  • Re: Caution in radp
    ... I was referring to multiple (many many multiple, yes, that is ... redundant) whacko / politically inflammatory posts. ... And the headers are usually three to four ... This is in addition to all the inflammatory / whacko responses. ...
    (rec.arts.disney.parks)
  • Re: Subroutine header
    ... I used to write headers like those, these days I lean more on my ... compilation and linking details. ... build for production release I (in line with site standards) strip out ... this unsuitable for source file headers anyway. ...
    (comp.lang.fortran)
  • Header include order
    ... - include system headers ... include application headers ... include the header associated with this source file ... Does anyone have a reasonable justification for the standard include ...
    (comp.lang.c)
  • Re: Larkin, Power BASIC cannot be THAT good:
    ... headers), and no platform-specific macros. ... one source file. ... doesn't have classes, Java doesn't have pointers, or half of C++'s OO ... So the only languages that allow 40-year old programs to be simply ...
    (sci.electronics.design)

Loading