I hope this hasn't been asked before on this site. I wasn't able to find a solid answer from google.
What happens when you #include iostream in multiple files of a project? I always use #ifndef and #define in my header files. Does that prevent iostream from being included more than once?
I had two interpretations of this question:
iostream
multiple times in the same compilation unit?
iostream
from be included multiple times?
#ifndef
and #endif
(or in a header marked with #pragma once
, in compilers that support it) from being seen by the compiler more than once... it is just what include guards does, right? But that is not really needed for #include <iostream>
, because it is already guarded from multiple inclusions.Like chris and Haroogan said, yes they will prevent that. What the #ifndef and #define are are pre-processor instructions, and are translated in english to
#ifndef (if undefined)
#define (define)
So if you make a header, and #include "myheader.h" twice, then the file will not be included again because you encompassed it in an if statement, which will cause the file to only be included(and defined) when it has not yet been included(defined).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With