24

So I have been trying to learn cpp and I was writing a program, and when I try to build the solution, it gives an error saying

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

Then I included it and I got the same error, and also another saying

cannot open source file pch.h

screenshot

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Druidswarm
  • 241
  • 1
  • 2
  • 3
  • Is pch.h at the same directory as they file you included? You should provide an verifiable example: https://stackoverflow.com/help/mcve. Any way, my guess is that the compiler is not finding `pch.h`. Please provide the code, or the concerned parts of it – geckos Jan 09 '19 at 01:18
  • 3
    This is the behaviour of Visual Studio compiler. When you are using 'Precompiled Headers' mode, you need to add `#include "pch.h"` at the beginning of your file *(as first include)* or disable Precompiled Headers as you see in the answer of selbie. – Julo Jan 09 '19 at 03:54
  • I don't think it's worth keeping this question/answer thread forever if this particular case was resolved. Does stack overflow have an option to prevent search engines from indexing uninteresting information ? – Johan Boulé Jul 27 '23 at 16:18

6 Answers6

45

One option, if you are new to c++, is to just turn off pre-compiled headers in the project settings.

selbie
  • 100,020
  • 15
  • 103
  • 173
3

It needs to be the first include, you can't place it under other includes.

2

Try adding the directory that your pch.h is in to the additional includes, even if it is at the root of your project. enter image description here

ZackOfAllTrades
  • 567
  • 5
  • 12
1

Your .cpp file is probably not in the same directory as pch.h

deanqx
  • 75
  • 1
  • 7
1

quick solution to a frustrating issue when trying to add .pch to an exisiting project: if you have a /include /src dir structure, it might not work, unless you place the "pch.h" and "pch.cpp" in the same dir /src.

Also: mark the "MyPreComp.cpp" as /Yc - create, and in the .cpp files you want to use the .pch set them to Yu - use.

#include "pch.h" as the first #include in the .cpp

NB. You need to set "not using precompiled headers" to all .cpp files not using them, yes, it IS a hassle.

( Visual Studio 2019 )

J.D-g8.1
  • 61
  • 5
1
  1. It needs to be included to each cpp file (by default)
  2. It needs to be included in the very first line of your code (excluding the comments, it's ok to have the fancy comments on top)
  3. It needs to be in a reachable directory. This error often happen when you have a folder structure in your project. So this can happen with a source files in some nested folder, when your precompile-header-file is up there in main. In this case, either add necessary number of "../" before the file name, or add the main folder to the "additional include directories" as it is already suggested above.
  4. It needs to actually be the same precompile header file, that is set as the one in project setting. Check the file with "Precompiled Header" option set to "Create (/Yc)", ensure that it refers to he same header file, that you include ("pch.h" or "stdafx.h" by default) This error often happens when you include some old source to newer proj, or vice-versa, due to different default names in different studio versions: "stdafx.h" vs "pch.h".
  5. If all above is set up, and you still have it, check if you actually set it up for the right build configuration. Always apply project setting change for all configurations. Costed me some nerves when I did it for only one config, and was trying to compile another: Check x86 vs x64 config