Re: #include question
From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/29/04
- Next message: Spacen Jasset: "Re: What is the difference in main"
- Previous message: Leor Zolman: "Re: What is the difference in main"
- In reply to: Dan Moos: "#include question"
- Next in thread: Dan Moos: "Re: #include question"
- Reply: Dan Moos: "Re: #include question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Spacen Jasset: "Re: What is the difference in main"
- Previous message: Leor Zolman: "Re: What is the difference in main"
- In reply to: Dan Moos: "#include question"
- Next in thread: Dan Moos: "Re: #include question"
- Reply: Dan Moos: "Re: #include question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|