472,805 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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

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
7 3473
"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
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
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

"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Tuckers | last post by:
My question is, if I have created my own library which lives in its own install directory, to refer to its header file is it better to use #include "MyLibrary.h" or #include <MyLibrary.h> ...
3
by: Arpi Jakab | last post by:
I have a main project that depends on projects A and B. The main project's additional include directories list is: ...\ProjectA\Dist\Include ...\ProjectB\Dist\Include Each of the include...
2
by: Susan Baker | last post by:
Hi, I am (trying) to compile some code I downloaded from the internet. The sources contain references to header files - using the form : #include <pathname/file> If I change the form to...
3
by: Kceiw | last post by:
Dear all, When I use #include "queue.h", I can't link it. The error message follows: Linking... G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In function `main':...
9
by: Richard Lionheart | last post by:
Hi All, I've got Visual Studio .Net installed, but I don't know it very well. So I tried to create a plain old Win32 using the command-line complier. I tried to compile: ************...
4
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() {...
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
4
by: saneman | last post by:
I have a folder 'app' containing a file 'main.cpp' and a subfolder 'types' (containing various header files). In main.cpp some header files from the subdir 'types' are included like: 1)...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.