Re: the use of #include <a_file.h> v/s #include"a_file.cpp"

Discussion in 'C++' started by Rolf Magnus, Nov 28, 2003.

  1. Rolf Magnus

    Rolf Magnus Guest

    mescaline wrote:

    > Hi,
    >
    > Suppose a_file.cpp contains a function a_function()
    > Now to include it in main_file.cpp I just do #include "a_file.cpp" and
    > I'm all set.
    >
    > i recently came across this seemingly roundabout way to do this in 3
    > steps:
    >
    > 1. Add in main_file.cpp
    > #include <a_file.h>
    >
    > 2. Then in a_file.h :
    >
    > #ifndef A_FILE_H
    > #define A_FILE_H
    > double a_function();
    > #endif
    >
    > 3. Finally in a_file.cpp:
    >
    > #include <a_file.h>
    > double a_function(){ actual implementation of the function...}
    > ------------------------
    > My questions:
    > 1. First, I cannot get this new method to work!
    > I have these three files. Then when I compile main_file.cpp, i get
    > the message: a_file.h not found. Why does this happen?


    #include <something> is for system headers, #include "something" for
    your own ones. So use quotes for it. The rest is ok.

    > Note that I'm using the plain g++ compiler here and all files are
    > in one directory (call it direct1). The book i read this method is
    > slightly unclear on where I should include all 3 files in the same
    > directory ...or somewhere else...
    >
    > 2. What is the use of such a roundabout way?
    > The a_file.h seems only to be a book-keeping file. I understand HOW
    > this new method works


    What do you mean by "new method"? It's the method everyone uses in C++.

    > but do not get the MOTIVATION behind this apparently roundabout method
    > -- has it got anything to do with reusability or something else?


    May I ask in which way this differs from what you were doing?
     
    Rolf Magnus, Nov 28, 2003
    #1
    1. Advertising

  2. Rolf Magnus

    mescaline Guest

    Rolf Magnus <> wrote in message news:<bq67k4$8gp$05$-online.com>...
    > mescaline wrote:
    >
    > > Hi,
    > >
    > > Suppose a_file.cpp contains a function a_function()
    > > Now to include it in main_file.cpp I just do #include "a_file.cpp" and
    > > I'm all set.
    > >
    > > i recently came across this seemingly roundabout way to do this in 3
    > > steps:
    > >
    > > 1. Add in main_file.cpp
    > > #include <a_file.h>
    > >
    > > 2. Then in a_file.h :
    > >
    > > #ifndef A_FILE_H
    > > #define A_FILE_H
    > > double a_function();
    > > #endif
    > >
    > > 3. Finally in a_file.cpp:
    > >
    > > #include <a_file.h>
    > > double a_function(){ actual implementation of the function...}
    > > ------------------------
    > > My questions:
    > > 1. First, I cannot get this new method to work!
    > > I have these three files. Then when I compile main_file.cpp, i get
    > > the message: a_file.h not found. Why does this happen?

    >
    > #include <something> is for system headers, #include "something" for
    > your own ones. So use quotes for it. The rest is ok.
    >
    > > Note that I'm using the plain g++ compiler here and all files are
    > > in one directory (call it direct1). The book i read this method is
    > > slightly unclear on where I should include all 3 files in the same
    > > directory ...or somewhere else...
    > >
    > > 2. What is the use of such a roundabout way?
    > > The a_file.h seems only to be a book-keeping file. I understand HOW
    > > this new method works

    >
    > What do you mean by "new method"? It's the method everyone uses in C++.
    >
    > > but do not get the MOTIVATION behind this apparently roundabout method
    > > -- has it got anything to do with reusability or something else?

    >
    > May I ask in which way this differs from what you were doing?


    HI, everybody again -- thaks for your replies.

    However my question is -- why the prototype.
    So far, if I needed to add extra functionality, I'd use only the
    headers as follows : #include "a_file.cpp" in my main_file.cpp
    The "new" way (for me), is writing #include <a_file.h> in
    main_file.cpp and then using this as a prototype for a_file.cpp which
    actually contains the code. My question is what is gained by adding
    the prototype?

    Also, how do I get this program to run? -- I don't have VC++ only a
    gcc complier
    -- putting the 3 files in one directory and compiling gives the same
    output as stated above
    thanks
     
    mescaline, Nov 28, 2003
    #2
    1. Advertising

  3. mescaline wrote:
    >
    >
    > However my question is -- why the prototype.
    > So far, if I needed to add extra functionality, I'd use only the
    > headers as follows : #include "a_file.cpp" in my main_file.cpp
    > The "new" way (for me), is writing #include <a_file.h> in
    > main_file.cpp and then using this as a prototype for a_file.cpp which
    > actually contains the code. My question is what is gained by adding
    > the prototype?


    David has already explained it, but let me show you an example
    taken from my current work:

    The project consists of aproximately 4400 *.cpp files. Compiling
    all of them takes between 1 and 1.5 hour.

    If I need to build a new executable, just because of correcting a
    bug, what do you think is better:

    * having to recompile all of the source code, because all those
    *.cpp files are included in one main.cpp and I need to feed
    main.cpp to the compiler (takes between 1 and 1.5 hours)

    * compiling just the *.cpp file which was fixed and linking all
    the individual pieces to form the executable (takes between
    10 seconds and a maximum of a few minutes).

    Which version would you chosse?


    >
    > Also, how do I get this program to run? -- I don't have VC++ only a
    > gcc complier
    > -- putting the 3 files in one directory and compiling gives the same
    > output as stated above


    You need to read your compilers documentation or ask in a newsgroup
    dedicted to gcc. But usually it's just:

    gcc fileA.cpp fileB.cpp fileC.cpp

    and gcc will compiler each of the source files individually and
    after that link them to form an executable.
    But consult your documentation to look for the details, since I don't
    use gcc by myself and typed the above from memory.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 28, 2003
    #3
    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. Danny Anderson
    Replies:
    5
    Views:
    516
    Victor Bazarov
    Aug 15, 2003
  2. Elie Nader
    Replies:
    1
    Views:
    649
  3. Tuckers
    Replies:
    18
    Views:
    632
    ThosRTanner
    May 18, 2005
  4. dirkheld
    Replies:
    8
    Views:
    409
    Rick Dooling
    Dec 5, 2007
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    957
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page