The purpose of stdafx.h (And: Why doesn't this work?)
I am working on a project that has a vendor-provided API. I've made a class that uses that API in my project and I've included the vendors header file in my stdafx.h
file. Things would not compile.
I then put the #include
directly into my class' header file and now things compile (And yes, my class includes stdafx.h
so that isn't the reason.
Do any of you have any guesses as to why it wouldn't compile the first time around? This isn't a project-stopper by far but I'd prefer if I could keep all vendor API files in stdafx.h where they belong开发者_Python百科.
EDIT: Problem solved, I'd created a cyclic dependency by forgetting to #ifndef a header file and then including them in the wrong order. I feel like an idiot.
stdafx.h
is mainly used in the VS generated projects as the 'container' of headers to be precompiled.
When you added a new #include
to stdafx.h
it didn't get included because your project is probably configured to use precompiled headers, and when you add something to stdafx.h
you need to regenerate the .pch file that contains the precompiled information.
One way to do that is to have a .cpp file in your project that does nothing but #include "stdafx.h"
. Maybe call it `precompile.cpp". Then go to the project settings for that one .cpp file and change the following setting (for all configurations):
"C/C++ | Precompiled Headers | Precompiled Header" setting
and select "Create /Yc"
.
That will set up the build so that when precompile.cpp
needs to be built (because the stdafx.h
header it includes has changed), it'll rebuild the .pch file that everything else uses.
EDIT: Wait - I don't think I read the question right. May still be helpful, though.
Another name for stdafx.h
is a 'Precompiled header'
There aren't really any 'vendor specifics' in stdafx.h
, what it does is it precompiles headers so that the compiler doesn't have to re-compile them every time you build the project.
It's only really helpful if you have a huge project (or a small one that includes tonnes of headers).
I use visual studio 2010 as well, generally it's not worth the fuss - I just disable it (which would solve your class inclusion issue also - make your own header, stick the vendor's in there).
精彩评论