Sign in to follow this  
Samsonite

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

Recommended Posts

If you mean like:


//Main.cpp
#include "SomeHeaderThatIncludesSomeOtherHeader.h"

void main(void)
{
//bla
}

//SomeHeaderThatIncludesSomeOtherHeader.h
#include <Iostream>





Then yes.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Samsonite
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]


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Samsonite
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]

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 this post


Link to post
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 this post


Link to post
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"?

Share this post


Link to post
Share on other sites
Thats what I did at first, but it created so many compile errors and I wanted to show something on screen, so I gave up.

Anyways, check my journal for the result [smile]

Thanks to all who helped me!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this