Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: the use of #include <a_file.h> v/s #include"a_file.cpp"

Reply
Thread Tools

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

 
 
Rolf Magnus
Guest
Posts: n/a
 
      11-28-2003
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?

 
Reply With Quote
 
 
 
 
mescaline
Guest
Posts: n/a
 
      11-28-2003
Rolf Magnus <(E-Mail Removed)> wrote in message news:<bq67k4$8gp$05$(E-Mail Removed)-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
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      11-28-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not use ''; file already in use. M K ASP .Net 11 04-09-2008 11:35 AM
where to use CPLD & where to use FPGA? kulkarku@math.net VHDL 6 03-06-2006 07:27 AM
How do I know when to use the Viewstate and when to use the posted data? :-) Simon ASP .Net 1 11-09-2004 02:32 AM
Can I use XPath or something to a remote Mac or Linux box and just query an xml file, not using web services and use encyrption? jake ASP .Net 0 07-06-2004 02:16 PM
Cannot use the profile "default" because it is in use, not. please.post@yur.re.ply Firefox 1 07-04-2004 03:41 AM



Advertisments