Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Remember to...

Name: Anonymous 2015-09-07 22:21

struct your code correctly so you do not have headers inside headers

As a rough guide:
1. If a file which contains nothing but "#include <header.h>" fails to compile, that header is broken. If a header needs a declaration to be in scope (e.g. for the type of an argument in a prototype declared in that header), it's the header's responsibility to perform the include, not the file including the header.

2. If the order in which headers are included matters, one or more of the headers is broken.

3. If a header can't be included (directly or indirectly) more than once, it's broken.

There are some situations where these rules don't apply, but they're particularly rare (e.g. using the preprocessor as a ghetto template system).

FWIW, the only place in the C standard library where the rules don't apply is <assert.h>, because you're supposed to be able to enable and disable asserts with
#undef NDEBUG
#include <assert.h>
// asserts enabled here
#define NDEBUG
#include <assert.h>
// asserts disabled here
#undef NDEBUG
#include <assert.h>
// asserts enabled here
// ... and so on

Name: Anonymous 2015-09-07 23:04

Name: Barney Strootstroop 2015-09-08 0:22

Anonymous, how do you reconcile points 1 and 2 with your previous statement, "struct your code correctly so you do not have headers inside headers"?

// included from header #2
void f(ABC *p);

// included from header #1
typedef int ABC;

int main(void) { }


Would you suggest 'avoid typedefs' or some other dogma, Anonymous?

3. If a header can't be included (directly or indirectly) more than once, it's broken.

#ifndef OKAY_I_WILL_KEEP_THIS_IN_MIND
#define OKAY_I_WILL_KEEP_THIS_IN_MIND
/* Okay, Anonymous. I will keep this in mind. */
#endif OKAY_I_WILL_KEEP_THIS_IN_MIND

FWIW, the only place in the C standard library where the rules don't apply is <assert.h>, because you're supposed to be able to enable and disable asserts with ...

I prefer to put the macro definition build script and enable it for the entire project, Anonymous. That way I don't end up with clumps of code that has assert always disabled and clumps of code that have it always enabled.

Name: Anonymous 2015-09-08 16:15

Headers shouldn't include other headers, it is the duty of the programmer to use something that is not C topologically sort all used headers.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List