c - Output Error When include file included twice -


[edit] seems people commenting , voting without reading post. please read before commenting. instance: if think against including files more once, missing point of post.

i trap sloppy programming outputting error when include files included twice.

i use #pragma once include files intend include within other include files. works great.

i know can variation of "guard macros" accomplish this. there better way? #pragma or gcc compile option?

this sample of guard macro:

#ifndef file_foo_seen #define file_foo_seen  entire file  #endif /* !file_foo_seen */ 

this work me:

#ifdef file_foo_seen  #error "file_foo inappropriately included twice.  #endif /* file_foo_seen */  #define file_foo_seen /* active code */ 

[edit] note: not asking why necessary support including header files multiple times. common case , supported gcc pragma. asking case when have specific include files not want included more once. if include them more once, 1 of 2 actions: 1) change trap #pragma once, or 2) change code eliminate dependency.

because there special pragma support multiple include, thought might have techniques avoid well.

[edit] add of rules follow related our use of header files:

  1. each .c file has companion .h file function prototypes. can think of .h file interface .c file. .h file may contain structure definitions , enums consumed or returned functions. .h file not need included in other .h file because other .h files not use functions.

  2. we not create companion .h file .c files contain main().

  3. sometimes structures , enums used independently of functions return or consume them. these cases enums , structures moved independent .h file. .h file contain #pragma once because included multiple times.

  4. opaque structures preferred.

  5. doxygen documentation documents interface contained in .h files. works because .h file contains complete interface .c file.

  6. we make exceptions these rules needed, part adhere these rules.

  7. following these rules makes writing unit tests easier complete interface easy identify , understand.

i don't think should (remind me not use headers, please, if decide go ahead scheme).

however, if start well-known mechanism preventing damage when header included twice:

#ifndef file_foo_seen #define file_foo_seen  …the entire file…  #endif /* !file_foo_seen */ 

then can adapt generate error when file is included twice (as recognize in question):

#if defined(file_foo_seen) #error file foo.h included #else #define file_foo_seen  …the entire file…  #endif /* file_foo_seen */ 

since #error part of standard c, should work — @ least diagnostic message output compilation. should, might not (solaris of yesteryear — i'm looking @ you!), prevent successful compilation.

depending on compilers, may able fine-tune error handling strategy, though frankly, best (by far) allow header included multiple times.

headers should self-contained , idempotent.

  • self-contained means should not required include else explicitly in order able use header (you write #include "header.h" , works).
  • idempotent means if include file twice, mechanism, works safely.

the c standard requires standard headers. strictly, <assert.h> isn't strictly idempotent, others idempotent. they're self-contained, though <inttypes.h> allowed include <stdint.h>.

you should follow lead of standard — provides techniques use.


related questions

i make no claim either complete list or unbiassed list of links.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -