Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   help with header files.. (http://www.velocityreviews.com/forums/t596432-help-with-header-files.html)

johnnash 03-06-2008 07:30 AM

help with header files..
 
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 ??

Nick Keighley 03-06-2008 08:06 AM

Re: help with header files..
 
On 6 Mar, 07:30, johnnash <johnnas...@gmail.com> 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






SM Ryan 03-06-2008 06:19 PM

Re: help with header files..
 
# 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.

Thad Smith 03-08-2008 06:33 AM

Re: help with header files..
 
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


All times are GMT. The time now is 01:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57