Stdafx.h cross platform no problem?

Hey, I followed the learning of learncpp.com over the last couple of days, they say to comment on "#include" stdafx.h "from .cpp files for Code :: Blocks.

Is it mandatory to remove the include line? What happens if you have hundreds of files and changed from Visual Studio on Win7 to Code :: Blocks on Linux or transfer it to someone else with a mac?

+6
c ++ cross-platform visual-studio precompiled-headers
source share
5 answers

stdafx.h is the idiomatic name used for precompiled headers in the Visual Studio ecosystem. In short, this is a common header, but the contents of this file will be compiled once and reused for all cpp files in the project.

This is useful because in most projects a large number of headers (standard library, system title, common definitions for the whole project) are used by almost all translation units (cpps), so using PCH is a huge performance benefit when compiling

(Actually, PCH is to crack the inefficient C ++ compilation and binding model, and it's a shame that we need to maintain manually ... oups, blasphemy.)

But it also means that - as long as the contents of your stdafx.h are gcc compatible - compiling with CodeBlocks should work, but without the immediate performance benefit.

stdafx.h , created by VS application wizards, does not work out of the box on other platforms - it usually includes Windows.h . Therefore, to make it work, protect certain Windows definitions with the appropriate #ifdef/#endif pairs and vice versa for Linux or Mac-specific things.

+10
source share

No, this tutorial doesn't make any sense. stdafx.h won't break anything at all. The system of precompiled headers in the Visual Studio compiler is specially designed in this way.

If your compiler supports precompiled headers (and follows the same pre-compilation method as Visual Studio), it can use stdafx.h to pre-compile.

If your compiler does not support precompiled headers (or uses a different approach to precompilation), then stdafx.h interpreted as a regular header file, different from any other header file, and treated like any other header file.

It is possible that this tutorial assumes that stdafx.h often includes some Windows-specific headers that are not presented on another platform. Although it is possible, it really has nothing to do with stdafx.h . Obviously, if you are compiling your program on some other platform, you should not try to include any Windows headers, no matter how you do it: via stdafx.h or elsewhere.

+4
source share

As far as I know, stdafx.h is just a Windows file (for precompiled headers): your code just won't compile unless you comment on it.

+1
source share

If you are not actually using a precompiled header (PCH), I suggest Visual Studio Options/Preferences->Precompiled Header to Visual Studio Options/Preferences->Precompiled Header and turning them off. If you try to remove them and still use Visual Studio, you will get a ton of errors.

+1
source share

The only thing to do is to include the path containing stdafx.h (or the precompiled header) in the default path list. This is necessary because the MS compiler actually replaces #include "stdafx.h" with precompiled data without a real header search.

Other compilers usually want to retrieve data. But it’s better not to comment. You can usually customize your compiler to also use precompiled header functions to speed compilation. Using gcc , which will be executed with the -pch option. With code blocks, I could find this wiki . Precompiled headers are not evil, on the contrary, they will save you valuable time if they understand it and use it adequately.

+1
source share

All Articles