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

6

28

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

Attitudinarian answered 9/1, 2019 at 1:6 Comment(3)
Is pch.h at the same directory as they file you included? You should provide an verifiable example: 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 itMosesmosey
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.Fyrd
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 ?Thrawn
A
48

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

Adamson answered 9/1, 2019 at 1:21 Comment(2)
What effect does turning off precompiled headers have? In other words, what breaks if you do this?Leaved
@GrahamLeggett - slower rebuild times. That's it.Adamson
G
3

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

Greatuncle answered 16/10, 2021 at 20:26 Comment(0)
W
3

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

While answered 28/7, 2022 at 6:32 Comment(1)
This solved the problem for me. I had it included in another header file and thought that it would work.Brandenburg
E
2
  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
Examine answered 23/12, 2022 at 8:52 Comment(0)
K
1

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

Karisa answered 13/8, 2021 at 23:4 Comment(0)
C
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 )

Chin answered 9/5, 2022 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.