Precompiled headers #inclusion strategy

Get answers to all your basic programming questions. No Ogre questions, please!
Pirs01
Gnoblar
Posts: 3
Joined: Mon Feb 18, 2013 1:40 pm

Precompiled headers #inclusion strategy

Post by Pirs01 »

Hello,

I'm new to concept of precompiled headers and confused about something in this tutorial where it says to "include stdafx.h in ALL your .h and .cpp files". Why would I do such a thing rather then include it where I need to just like I would if I didn't use precompiled header?

EDIT: Also from what I understand the stdafx.cpp is just a placeholder used to generate the precompiled header. Do I need it? Can I not include stdafx.h in my A.h and then do with A.cpp (my actual source file rather then a place holder) what the tutorial tells to do with stdafx.cpp?

Thank you
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Precompiled headers #inclusion strategy

Post by bstone »

You certainly don't have to include stdafx.h in your .h files. You should have it as your first #include directive in your .cpp files usually. The reason is that this #include directive works as a way to tell your compiler that up to this point all other #include directives are considered to be compiled as part of the header precompilation process and the compiler can safely ignore them (thus speeding up the compilation).

You could use your A.cpp instead of stdafx.cpp as long as you would be happy with your precompiled headers being rebuilt each time you update A.cpp. Not a good idea in general.
Pirs01
Gnoblar
Posts: 3
Joined: Mon Feb 18, 2013 1:40 pm

Re: Precompiled headers #inclusion strategy

Post by Pirs01 »

Thank you but still need a clarification on the first topic.

If I understand you correctly then this is what I should do in given scenario:

I have A.h, A.cpp, B.h, B.cpp. A.h includes all kinds of Ogre stuff I want to precompile. B.h include A.h.
I move Ogre includes to stdafx.h and I want it in A.h but since it's a compiled code a header file is not a place for it to go. Instead I include it directly in A.cpp where it will be placed after the A.cpp itself is compiled.
Now, the B.cpp includes B.h which includes A.h but it does not include stdafx.h and so B.cpp (given that it needs Ogre stuff) must also include stdafx.h directly.

In other words including precompiled headers in .h files is not allowed and the cpp files that require precompiled stuff need to inlude it explicitly. Is that correct?

However doesn't it make a copy of my precompiled code every time I include it in another cpp file? Is it something for linker to worry about?

Sorry for making this more complicated then it actually is :)
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Precompiled headers #inclusion strategy

Post by bstone »

Yeah, it might be easier for you to think about precompiled headers as a project-wide decision - you either use them everywhere throughout your project or you don't use them. You could mix them on and off if really needed but that might introduced its own issues.

In your case you have two options: 1) add #include "stdafx.h" to the top of B.cpp and enjoy it; 2) disable the use of precompiled headers in B.cpp properties (i.e. adjust related C++ compiler options) and let it suck all required headers in from A.h and compile everything again the traditional way (but make sure you list every required #include in A.h).

Also, the precompiled headers don't go into A.cpp. They go into .pch files (the default is one per project).
Pirs01
Gnoblar
Posts: 3
Joined: Mon Feb 18, 2013 1:40 pm

Re: Precompiled headers #inclusion strategy

Post by Pirs01 »

bstone wrote:Also, the precompiled headers don't go into A.cpp. They go into .pch files (the default is one per project).
Yes, not quit what I meant. Little shortcut I took there, never mind though. The point of it was that you don't put stdafx.h in header files.

Thanks for help.