By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
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 but do not get the MOTIVATION behind this
apparently roundabout method -- has it got anything to do with
reusability or something else?

Thanks for your answers,
m
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"mescaline" <ap*****@columbia.edu> wrote:
i recently came across this seemingly roundabout way to do this in 3
steps:

1. Add in main_file.cpp
#include <a_file.h>
You should use double quotes, like this: "a_file.h"
The angle brackets <> are used for including system headers like <set> and
<list>
2. Then in a_file.h :

#ifndef A_FILE_H
#define A_FILE_H
double a_function();
#endif
This is called a "prototype": it gives the name, parameters and return type
of the function,
but not the implementation (body of the function).
3. Finally in a_file.cpp:

#include <a_file.h>
double a_function(){ actual implementation of the function...} My questions:
The book i read this method is slightly unclear on
where I should include all 3 files in the same directory ...or
somewhere else...
The same directory - it should work OK (even with angle brackets).
2. What is the use of such a roundabout way?


Maybe you are used to using a programming language where all of the code is
in a single file,
and always gets compiled at the same time. C and C++ both allow "separate
compilation",
where different parts of the program can be compiled at different times.

It is really important to be able to do this. Say you have a 1000000 line
program, and you
want to change a displayed string from "Hello" to "Hi". It would be a pain
to have to
recompile all of the code instead of just the small part that is really
affected.

But once your program is split into several files, they need to know about
each other
(at least enough to call functions in other files). That's what #include is
for - you can
declare functions that are defined in a file so that functions in other
files "know about them"
and can call them.

The problem with saying #include "a_function.cpp" is that if it appears in
two different
files (say a.cpp and b.cpp), the compiler will say that a_function() is
multiply defined -
once in a.cpp and once in b.cpp.

The only things that should appear in an included file are declarations
(like function
prototypes and classes), never definitions. The exception to this is
"inline" functions
and templates, but don't worry about them for now.

David F

Jul 22 '05 #2

P: n/a
Hi,

"mescaline" <ap*****@columbia.edu> wrote in message
news:e5**************************@posting.google.c om...
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> Use
#include "a_file.h"
instead

2. Then in a_file.h :

#ifndef A_FILE_H
#define A_FILE_H
double a_function();
#endif standard procedure..
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! It isn't new.
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? In almost every compiler you have to tell where to look. In VC++ use options
in g++ use -I<directory>
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... Same directory is fine (use -I option)
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 but do not get the MOTIVATION behind this
apparently roundabout method -- has it got anything to do with
reusability or something else?
..h files contains definitions structures etc. This lets the compiler issues
warnings if you make mistakes in your calls, use of structures etc.

Thanks for your answers,
m

No thanks.

Regards, Ron AF Greve
Jul 22 '05 #3

P: n/a
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?

Jul 22 '05 #4

P: n/a

"mescaline" <ap*****@columbia.edu> wrote in message
news:e5**************************@posting.google.c om...
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?
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 but do not get the MOTIVATION behind this
apparently roundabout method -- has it got anything to do with
reusability or something else?

Thanks for your answers,
m


obviously, the code that you typed will not work for a very simple reason:
#include <something> is used for libraries defined in C++.
#include "A_file.cpp"
#include "A_file.h" are used to user defined files.
so?...you cannot include files the same way that you include libraries.
i.e., you can't #include <A_file.cpp>

the reason we split the codes this way has two dimensions;
the first was very well explained by "David F"
the second reason; business wise,
say you have implemented a file for a virtual car that will be used a game.
and you worked on this file for 1 full year. You sold this file for a
company. now the question!! are you gonna give the company full access to
the whole code that you worked on? they might re-use your code.
they might modify it, and then re-sell it.....hummm...so what to do?
you create a header file including all the function of the car, like
acceleration, brake, turn left, turn right...and then, in an encrypted file,
you will put all the implementation.
in this way, you are sure that no one will mess with YOUR code.
conclusion, this roundabout way that you didn't like will protect you file
from being stolen AND will make it easier to change any variable in it.

Eliahooo
Jul 22 '05 #5

P: n/a
Rolf Magnus <ra******@t-online.de> wrote in message news:<bq*************@news.t-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
Jul 22 '05 #6

P: n/a
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
kb******@gascad.at
Jul 22 '05 #7

P: n/a
"Elie Nader" <el*******@sympatico.ca> wrote in message news:<5M*********************@news20.bellglobal.co m>...
"mescaline" <ap*****@columbia.edu> wrote in message
news:e5**************************@posting.google.c om...
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?
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 but do not get the MOTIVATION behind this
apparently roundabout method -- has it got anything to do with
reusability or something else?

Thanks for your answers,
m


obviously, the code that you typed will not work for a very simple reason:
#include <something> is used for libraries defined in C++.
#include "A_file.cpp"
#include "A_file.h" are used to user defined files.
so?...you cannot include files the same way that you include libraries.
i.e., you can't #include <A_file.cpp>

the reason we split the codes this way has two dimensions;
the first was very well explained by "David F"
the second reason; business wise,
say you have implemented a file for a virtual car that will be used a game.
and you worked on this file for 1 full year. You sold this file for a
company. now the question!! are you gonna give the company full access to
the whole code that you worked on? they might re-use your code.
they might modify it, and then re-sell it.....hummm...so what to do?
you create a header file including all the function of the car, like
acceleration, brake, turn left, turn right...and then, in an encrypted file,
you will put all the implementation.
in this way, you are sure that no one will mess with YOUR code.
conclusion, this roundabout way that you didn't like will protect you file
from being stolen AND will make it easier to change any variable in it.


You got some good information, some bad information.

The use of angle brackets around an include filename tells the system
to look for the file in the "system" directories. The use of quotes
around an include filename tells the system to first look at files in
user-defined directories, and if not found then look in the "system"
directories. These directories are defined by -I flags on the command
line (which I take it you are not using). If your files are in some
other directory (as is likely: the one you are developing in) you
would need to add a parameter to the compile command line to include
the development directory in the search list defined by the -I flag.
Alternatively, you can include the full path to your user directory in
the include statement. If you were developing in the directory
E:\mystuff you would code the include as follows:
#include "E:\mystuff\a_file.h"

The reasons for separating the header file from the source are
correct. It allows distribution of the functions by shipping the
header file and an object file containing the code -- but not
releasing the source.
--
Gary
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.