Public Group

# If I include a file in main that includes iostream. Will main contain iostream?

This topic is 4545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hehe, there's no more to say [grin] EDIT: It will, right?

##### Share on other sites
[edited out] yeah (if i understand you correctly)

##### Share on other sites
If you mean like:

//Main.cpp#include "SomeHeaderThatIncludesSomeOtherHeader.h"void main(void){//bla}//SomeHeaderThatIncludesSomeOtherHeader.h#include <Iostream>

Then yes.

##### Share on other sites
So if I do like this:

Main (contains) "Game.h" (which contains) "SDLManager.h" (which contains) "Globals.h" (which contains) "Includer.h" (which contains) <iostream>, <SDL/SDL.h> and <string>.

That would work?

Phew, that was a long list of includes [grin]

##### Share on other sites
Quote:
 Original post by SamsoniteSo if I do like this:Main (contains) "Game.h" (which contains) "SDLManager.h" (which contains) "Globals.h" (which contains) "Includer.h" (which contains) , and .That would work?Phew, that was a long list of includes [grin]

If I understand that list correctly, then yes, you will have access to whatever the Game Header File includes...

##### Share on other sites
Yes, that would work.

The preprocessor goes through the code, and whenever it finds an #include <file> it's as if it copies and pastes the code in that header into the spot where it finds the #include. It doesnt matter how many includes you have, because it all effectively goes into one big file by the end.

With gcc there are options so that you can see the compiler output on just running the preprocessor, without doing any further processing. If your unsure about exactly how preprocessor directives work, its worth taking a glance.

##### Share on other sites
Quote:
 Original post by SamsoniteSo if I do like this:Main (contains) "Game.h" (which contains) "SDLManager.h" (which contains) "Globals.h" (which contains) "Includer.h" (which contains) , and .That would work?Phew, that was a long list of includes [grin]

Yes. Which is why you should avoid unneccessary includes within headers. It creates unneccessary dependancies. Eventually, you end up in a situation where changing even a single header results in your entire project needing to be recompiled even though only one file really cares about the change. You should also not assume other files include things. If main requires std::string, then main.cpp should include <string>. If it gets included twice, oh well, that's what include guards are for. But if you end up removing <string> from some other header, you don't want to break main.cpp.

CM

##### Share on other sites
If I recall, leaving such a long path of includes to get what you want might not be the best option. Altering some part on that path can lead to problems that might not be easy to figure out very quickly. Someone can correct me if I'm wrong.

##### Share on other sites
Quote:
 Original post by Conner McCloud... If it gets included twice, oh well, that's what include guards are for.CM

You mean something like:

"#pragma once"?

or

#ifndef _FOO_H_
#define _FOO_H_

...

#endif

1. 1
2. 2
3. 3
frob
12
4. 4
5. 5

• 13
• 14
• 65
• 14
• 15
• ### Forum Statistics

• Total Topics
632130
• Total Posts
3004286

×