How do I fix unexpected end of file error with pch.h

Viewed 26268

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

5 Answers

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

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

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

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

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 )

Related