DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: #include

  1. #1
    Dan I Guest

    #include


    Attention everyone: get the newbie!

    Allright, if header file # 1 looks like this (and is named Header1.h):

    #include "stdafx.h"
    #include <iostream.h>

    And header file #2 looks like this:

    #include "Header1.h"

    Does header file #2 automatically "inherit" (<-I know:not really) what the
    first header file included?
    Does this "daisy-chaining" apply to header files?

    Thanks- Dan I

    "The only stupid question is the one you don't ask."



  2. #2
    Randy Charles Morin Guest

    Re: #include

    The word inherit is misleading. But, yes it does. Inclusion of files with
    the C++ preprocessor is transitive. If A includes B, then it is included in
    place. If B includes C, then A also includes C. There are exceptions where
    preprocessor conditionals are used. Here's an example of transitive
    inclusion.
    ----------
    File a.h
    ----------
    lalalalala

    ----------
    File b.h
    ----------
    #include "a.h"

    ----------
    File c
    ----------
    #include "b.h"
    falalalala

    The end result is
    lalalalala
    falalalala
    --
    Randy Charles Morin
    Author of Programming Windows Services
    http://www.kbcafe.com

    Feel free to contact me by private email or messenger
    MSN Messenger - morin_randy@hotmail.com
    Yahoo Messenger - randymorin@yahoo.com

    "Dan I" <mudfan66@hotmail.com> wrote in message
    news:3b4b0704$1@news.devx.com...
    >
    > Attention everyone: get the newbie!
    >
    > Allright, if header file # 1 looks like this (and is named Header1.h):
    >
    > #include "stdafx.h"
    > #include <iostream.h>
    >
    > And header file #2 looks like this:
    >
    > #include "Header1.h"
    >
    > Does header file #2 automatically "inherit" (<-I know:not really) what the
    > first header file included?
    > Does this "daisy-chaining" apply to header files?
    >
    > Thanks- Dan I
    >
    > "The only stupid question is the one you don't ask."
    >
    >




  3. #3
    Danny Kalev Guest

    Re: #include

    first of all, you seem to be confusing two concepts: a header file and a
    source file. A header file is the one that gets #included in a source
    file. The former has the .h extension whereas the latter has the .cpp,
    ..c., .cxx and so forth extensions.

    Now to your questions: including a header file means that the
    preprocessor (a program that reads the physical source and header files
    and produces a file that the compiler compiles) inserts its content into
    the file that #includes it. If you have nested #includes, that is, a
    header file which #includes another header file, the inclusion is
    recursive: the deepest header file is inserted into the header file that
    #incldues it and then the result is inserted into the source file.

    Danny

    Dan I wrote:
    >
    > Attention everyone: get the newbie!
    >
    > Allright, if header file # 1 looks like this (and is named Header1.h):
    >
    > #include "stdafx.h"
    > #include <iostream.h>
    >
    > And header file #2 looks like this:
    >
    > #include "Header1.h"
    >
    > Does header file #2 automatically "inherit" (<-I know:not really) what the
    > first header file included?
    > Does this "daisy-chaining" apply to header files?
    >
    > Thanks- Dan I
    >
    > "The only stupid question is the one you don't ask."


  4. #4
    Ralph Guest

    Re: #include



    To amplify what Danny said...

    The compiler works on "Translation Units". Since it appears you are using
    CL, each specified source file on the command line is a separate translation
    unit.

    Example: "cl a.c b.c d.c" will compile each source file in turn, creating
    a .obj file for each which is then linked into a single product. You therefore
    have three translation units. After each of these units are compiled the
    linker will then try and make sense out of all this.

    Some compilers allow the '+' option (cc a.c + b.c + d.c) which will concatenate
    all source files into one temp file and then compile the whole thing, but
    M$ is not one of them. Each source file on the compile line becomes a separate
    translation unit. All the #includes, #pragmas, #imports, defined in that
    translation unit will be concatentated into one 'source' file which will
    then be compiled just as if you had typed the contents of each of these files
    into the source file.

    (By the way you can just as easily #include a source file, #include "a.cpp".
    There is nothing magically about headers.

    The concept of a translation unit is important. It defines scope and linkage.
    Any problems you have, for example, 'xxx' not found, will be directly related
    to how you have setup each translation unit.


    "Dan I" <mudfan66@hotmail.com> wrote:
    >
    >Attention everyone: get the newbie!
    >
    >Allright, if header file # 1 looks like this (and is named Header1.h):
    >
    >#include "stdafx.h"
    >#include <iostream.h>
    >
    >And header file #2 looks like this:
    >
    >#include "Header1.h"
    >
    >Does header file #2 automatically "inherit" (<-I know:not really) what the
    >first header file included?
    >Does this "daisy-chaining" apply to header files?
    >
    >Thanks- Dan I
    >
    >"The only stupid question is the one you don't ask."
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center