threads / discuss / 29199

header file at a top

Subject: header file at a top

## tl;dr

2 messages between Dec 17, 2011 and Dec 18, 2011.

replies: 1people: 2as markdown or json

Thiago Farina· Dec 17, 2011, 21:57 UTC · lore
Hi folks,

I don't understand why in git the .h file is not included as the first substantive line of the .c file.

i.e:

foo.c -------------------------------------------------------------------- #include "foo.h"

/* in alphabetical order */ #include "bar.h" #include "tiz.h"

Any particular reason we include the corresponding header file at some random position in the source file?

Jeff King· Dec 18, 2011, 05:43 UTC · re: Thiago Farina · lore

Re: header file at a top

On Sat, Dec 17, 2011 at 07:57:42PM -0200, Thiago Farina wrote:
Show 15 quoted lines
> I don't understand why in git the .h file is not included as the first
> substantive line of the .c file.
> 
> i.e:
> 
> foo.c
> --------------------------------------------------------------------
> #include "foo.h"
> 
> /* in alphabetical order */
> #include "bar.h"
> #include "tiz.h"
> 
> Any particular reason we include the corresponding header file at some
> random position in the source file?

The first include in any git source file should be either "git-compat-util.h", or something that includes it (usually "cache.h"). This introduces compatibility wrappers and definitions which may be used by other headers.

After that, the next one could be "foo.h", and I do tend to like that style (since it gives a check that foo.h has no surprising header dependencies). But I think we simply don't bother caring about header dependencies in git (i.e., we do have recursive includes in some places, but we don't require them, and in practice the compiler will tell us if a source file fails to include a depended-upon header).

-Peff

← back to recent threads