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:
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.
we not create companion .h file .c files contain main().
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.opaque structures preferred.
doxygen documentation documents interface contained in .h files. works because .h file contains complete interface .c file.
we make exceptions these rules needed, part adhere these rules.
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.
- should use #include in headers?
- how link multiple implementation files in c
- how structure #includes in c
- what reference documenting patterns of use of ".h" files in c?
- c how manage #include relationship among multiple source files , create correct makefile
- breaking large c program in header files , c files
- repeated typedefs - invalid in c valid in c++?
Comments
Post a Comment