I would like to trap sloppy programming by outputting an error when some include files are included twice.
I use #pragma once for include files that intend to include within other include files. That works great.
I know I can do a variation of "guard macros" to accomplish this. But is there a better way? Another #pragma or a gcc compile option?
This is a sample of a guard macro:
#ifndef FILE_FOO_SEEN
#define FILE_FOO_SEEN
the entire file
#endif /* !FILE_FOO_SEEN */
This would work for me:
#ifdef FILE_FOO_SEEN
#error "FILE_FOO inappropriately included twice.
#endif /* FILE_FOO_SEEN */
#define FILE_FOO_SEEN
/* Active Code */
[edit] Note: I am not asking for why it is necessary to support including header files multiple times. That is a common case and supported with the a gcc pragma. I am asking for the case when I have specific include files that I do not want included more than once. If someone were to include them more than once, then it would be one of two actions: 1) Change the trap to a #pragma once, or 2) change the code to eliminate the dependency.
Because there is a special pragma to support multiple include, I thought someone might have good techniques to avoid this as well.
Aucun commentaire:
Enregistrer un commentaire