Can I use #include "pch.h" instead of #include "stdafx.h" as my precompile header in Visual Studio C++?

35,817

Solution 1

The default precompiled headers name was stdafx.h for several years now. Lately, with VS 2017, they changed the default name the project wizard creates to pch.h. I don't know why but they did it.

You can adjust the name of the precompiled header file in the Project Properties under Configuration Properties -> C/C++ -> Precompiled Headers. You can also enable or disable the use of them there. You can even define this on a per file basis. You can even configure multiple different Precompiled Header files in one project.

So for you concrete question: There won't be any kind of issue replacing stdafx.h with pch.h in your tutorial. The tutorial is probably just older and hasn't been updated yet.

Solution 2

Try moving "#include pch.h" to the very top of the file (before any other headers).

Some compilers won't compile content before the pre-compiled header file(s) by default. This setting can be changed though.

Solution 3

In the learncpp.com tutorials, the instructor(s) recommend(s) us to turn off the "Precompiled Header" additional option when creating a new C++ project using the Windows Desktop Wizard option on Visual Studio. And it is a good idea to do so while we haven't been introduced to Header files.

However, pch.h seems to be the "new" stdafx.h, so it is not that you can use the former instead of the latter, but that you have to, at least if choosing the option of "Precompiled Header".

Share:
35,817
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    This is my first time working with C++ and properly getting into coding. I'm following the C++ tutorials on learncpp.com and I'm using Visual Studio 2017...

    In the tutorial they start off with some simple "Hello, world!" code and at top of the code they put #include "stdafx.h" along with #include <iostream>. When I replicate this code myself and try to build it, I get the error:

    C1010: unexpected end of file while looking for precompiled header. Did you forget to add #include "pch.h" to your source?

    When looking at my solution explorer I noticed that in their tutorial in the header and source files tab they have files called "stdafx.h" and "stdafx.cpp", but for me those files are called "pch.h" and "pch.cpp".

    So I then tried renaming the #include "stdafx.h" to #include "pch.h" and the code was built and executed perfectly. So should I just stick with #include "pch.h" for the rest of my code or is this some kind of issue?

    Thanks!

  • D.C. the III
    D.C. the III over 5 years
    Best answer here. Considering the OP is new to C++ getting bogged down by the complexity of headers this early is probably not a good approach. I'm in the same boat as a beginner and realize until I get to that chapter in my textbook it is better to just use the Console Wizard.
  • FindOutIslamNow
    FindOutIslamNow over 5 years
    Thank you, although it is included I had to move them on top
  • Franky
    Franky over 5 years
    I faced the similar error and after reading this answer I disabled the use pch. My question is that is is harmless to disable it?
  • marc40000
    marc40000 over 5 years
    @Franky: With pchs you can optimize the build time of your code. That's all. Enabling or disabling them will not degrade your code in any other way.
  • Marc.2377
    Marc.2377 almost 5 years
    Because *afx is a MFC standard and as such should be dead already.
  • drescherjm
    drescherjm about 4 years
    If your code is not very complex and does not include many external libraries using precompiled headers can actually make the compile times a longer. This feature is most useful if you have thousands of lines of code and many header dependencies such that building your code takes minutes.