what's with stdafx.h?

Started by
5 comments, last by Promethium 15 years, 2 months ago
Hello, I have a question about precompiled headers, and more importantly about the #include "stafx.h" statement. Doesn't this undermine the portability of libraries you create? Maybe my project settings are wrong, but the compiler complains if i don't include stdafx in all .cpp files. To make things work properly, i now have 4 files: main.cpp: #include "stafx.h" int main{ foo somevariable; } stafx.h: #include "foo.h" foo.h: class foo{ public: foo(); ~foo() }; foo.cpp: #include "stdafx.h" foo::foo(){}; foo::~foo(){}; As you can see, foo.cpp doesn't include foo.h, I suppose this a problem when I want to use this class in an other compiler which doesn't use the stdafx.h precompiled header?
Advertisement
In the application wizard, just check "Empty project". If you are making a static library, uncheck "Precompiled header" instead.
-----------------------------------------Everyboddy need someboddy!
You shouldn't include foo.h in stdafx.h. Stdafx.h is for "rarely changing headers" which translates to third-party libraries you need. For example, windows.h, STL headers, boost, etc. Stdafx.h is compiled once for your project, and then reused in all your compilation units (cpp files). This speeds the compilation up when you include many headers.

But if you include foo.h then stdafx.h must be recompiled every time you change foo.h or any headers it depends on, which remove any advantage.

If you really cant make precompiled headers work you can disable them in the project settings, but you should make an effort to learn to use them; they are really nice for large projects.
Oh, and one more thing: stdafx.h != precompiled headers. Stdafx.h and .cpp are just ordinary code files, just like any others. But the precompiled headers are constructed from stdafx.h. So if you distribute your sources to other it will still work just like in Visual Studio, they will just not get the compilation speed-up from the precompiled headers.
Most of the code I write at work gets built on windows and linux so I always disable precompiled headers in the project properties and delete the stdafx.h file.

The option is in Configuration Properties -> C/C++ -> Precompiled Headers -> Create/Use Precompiled Header
So it's just a question of project settings? If i create a new project and select empty project, there are no precompiled headers and I need to include foo.h in foo.cpp and foo.h in main.cpp. Since there is no stdafx.h, i guess the compiler won't complain if i don't include it, but the project will compile slower because i include iostream which needs to be recompiled each time.

Now, how do i get the best of both worlds: precompiled headers plus the ability to only include foo.h and not stdafx.h into foo.cpp? Or is there no such thing?
Just include both in foo.cpp:
#include "stdafx.h"#include "foo.h"/* your code here */

Visual studio will complain if stdafx.h is not the first file included in a cpp file to ensure that headers are included in the correct order. However, you can include as many files as you like in a compilation unit (or header).

This topic is closed to new replies.

Advertisement