D
Derrick Coetzee
It seems like, in every C source file I've ever seen, there has been a
very definite include order, as follows:
- include system headers
- include application headers
- include the header associated with this source file
For example, in a file hello.c:
#include <stdio.h>
#include "utils.h"
#include "hello.h"
(Incidentally I think that a source file which doesn't include the header
file which exports its symbols is _very_ bad, as this is a good way to
check for inconsistencies for free.)
I would argue that the standard order of header including is wrong,
and that the correct order is the reverse. Consider this scenario:
hello.c:
#include <stdlib.h>
#include "hello.h"
hello.h:
struct blah {
size_t size;
};
hello2.c
#include "hello.h"
Inexplicably (from the perspective of the person doing the including)
the file hello.h will cause compiler errors in hello2.c but not in hello.c.
If hello.c were written first, and then the include file used elsewhere,
the error would appear to be "new", and not be caught by those who wrote
hello.c, implementing the functionality exported by hello.h.
If this include order is used, this problem is averted:
- include the header associated with this source file
- include application headers
- include system headers
This is good for two reasons:
1. All headers must now include any system headers they need, and will
fail immediately if they don't.
2. Every header will be included in at least ONE source file before
anything else (the source file associated with that header), allowing
any intra-application dependencies to be caught.
Does anyone have a reasonable justification for the standard include
order that I haven't thought of? Thanks.
very definite include order, as follows:
- include system headers
- include application headers
- include the header associated with this source file
For example, in a file hello.c:
#include <stdio.h>
#include "utils.h"
#include "hello.h"
(Incidentally I think that a source file which doesn't include the header
file which exports its symbols is _very_ bad, as this is a good way to
check for inconsistencies for free.)
I would argue that the standard order of header including is wrong,
and that the correct order is the reverse. Consider this scenario:
hello.c:
#include <stdlib.h>
#include "hello.h"
hello.h:
struct blah {
size_t size;
};
hello2.c
#include "hello.h"
Inexplicably (from the perspective of the person doing the including)
the file hello.h will cause compiler errors in hello2.c but not in hello.c.
If hello.c were written first, and then the include file used elsewhere,
the error would appear to be "new", and not be caught by those who wrote
hello.c, implementing the functionality exported by hello.h.
If this include order is used, this problem is averted:
- include the header associated with this source file
- include application headers
- include system headers
This is good for two reasons:
1. All headers must now include any system headers they need, and will
fail immediately if they don't.
2. Every header will be included in at least ONE source file before
anything else (the source file associated with that header), allowing
any intra-application dependencies to be caught.
Does anyone have a reasonable justification for the standard include
order that I haven't thought of? Thanks.