Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > help with header files..

Reply
Thread Tools

help with header files..

 
 
johnnash
Guest
Posts: n/a
 
      03-06-2008
ok so i was just reading about #ifndef and header files. I have a few
doubts. It would be great help if someone can clear my doubts..

#ifndef A_H
#define A_H
....
code
.....
#endif

^^ lets say A.h is included in B.h which is included in C.h..so how
will the compiler resolve everything ??

also i saw some programs where there are many files like a.h, a.c,
b.h, b.c, c.h, c.c etc and then there is a all.h file
which goes like this -

#ifndef A_H
#include "a.h"
#endif

#ifndef B_H
#include "b.h"
#endif

#ifndef C_H
#include "c.h"
#endif


then only all.h is included in a.c, b.c and c.c

how did it work ??
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      03-06-2008
On 6 Mar, 07:30, johnnash <(E-Mail Removed)> wrote:

> ok so i was just reading about #ifndef and header files. I have a few
> doubts. It would be great help if someone can clear my doubts..


what are your "doubts"? You aren't clear about what is unclear!

#include "xyz.h"

simply means "include the text of the file xz.h here"


> #ifndef A_H


if A_H (a preprocessor macro) is not defined then include (see above)
the following text.


> #define A_H


define the preprocessor macro A_H. Subsequent
#ifndef A_H will not "fail"- that is *not* include
the following text


> ...
> code
> ....
> #endif


marks the end of the text taht is included after a "successful"
#if (or #ifdef or #ifndef etc).



> ^^ lets say A.h is included in B.h which is included in C.h..so how
> will the compiler resolve everything ??


what is the problem? Suppose D.c includes C.h and you compile D.c
This is what the compiler sees after the preprocessor has been.
I've added indents to make it clearerer. I've skiiped A.h as I got
bored


D.c
#include C.h
#ifndef C_H
#define C_H
#include B.h
#ifndef B_H
#define B_H
B code
#endif
C code
#endif

I don't see the problem


> also i saw some programs where there are many files like a.h, a.c,
> b.h, b.c, c.h, c.c etc and then there is a all.h file
> *which goes like this -
>
> #ifndef A_H
> #include "a.h"
> #endif


this simply does the test outside the header a.h file.
It arguably makes for a faster compile (a.h is never
openened) but I find it harder to administer and harder
to read (zillions of #ifndefs to amnage)


> #ifndef B_H
> #include "b.h"
> #endif
>
> #ifndef C_H
> #include "c.h"
> #endif
>
> then only all.h is included in a.c, b.c and c.c
>
> how did it work ??


again I don't see the problem...
a.c includes all.h which includes a.h, b.h and c.h.

Is it the recursion that bothers you. An include file
including an include file?

Try working through some examples on paper


--
Nick Keighley





 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      03-06-2008
# also i saw some programs where there are many files like a.h, a.c,
# b.h, b.c, c.h, c.c etc and then there is a all.h file
# which goes like this -

Put the guards for file x.h inside file x.h, and everything else
inside the guards.

file a.h:
#ifndef A_H
#define A_H
#include "b.h"
#include "c.h"

declarations of a.h
#endif
file b.h:
#ifndef B_H
#define B_H
#include "a.h"
#include "c.h"

declarations of b.h
#endif
file c.h:
#ifndef C_H
#define C_H
#include "a.h"
#include "b.h"

declarations of c.h
#endif

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Title does not dictate behaviour.
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      03-08-2008
SM Ryan wrote:
> # also i saw some programs where there are many files like a.h, a.c,
> # b.h, b.c, c.h, c.c etc and then there is a all.h file
> # which goes like this -
>
> Put the guards for file x.h inside file x.h, and everything else
> inside the guards.
>
> file a.h:
> #ifndef A_H
> #define A_H
> #include "b.h"
> #include "c.h"
>
> declarations of a.h
> #endif
> file b.h:
> #ifndef B_H
> #define B_H
> #include "a.h"
> #include "c.h"
>
> declarations of b.h
> #endif
> file c.h:
> #ifndef C_H
> #define C_H
> #include "a.h"
> #include "b.h"
>
> declarations of c.h
> #endif


What is the purpose of including each other?

If you choose to use the style in which header files include other header
files, you should either
1. use the "all.h" style (which I seldom use) or
2. have each include file include the other files that are needed for
dependencies in the declaration within the outside header file.

Your example isn't the first case. And if each header file (a.h, b.h and
c.h) required declarations from both of the other files, they are designed
poorly. Dependencies should be hierarchical, not circular.

--
Thad
 
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
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
Header files included in header files John Smith C Programming 18 07-24-2004 04:55 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM
how to avoid using another header file inside a header file? Newsgroup - Ann C++ 4 11-02-2003 01:20 PM



Advertisments