c++build-errorprecompileprecompiled-headersvcxproj

C++ "error C1010: unexpected end of file while looking for precompiled header" when adding new .h and corresponding .cpp just for new files


Background...

Problem...

Question...

I've tried a number of different combinations to try to get this to work, but I am confused why Foo is not giving the same problems that Bar is. Is there some subtlety that I am missing? What sneaky difference should I be looking for? From all I can tell, everything relevant to this is set up the same way for both Foo and Bar.

What I've Tried, and What I Expect...

I've tried shuffling around the include statements (many attempts at which resulted in piles of nasty errors that indicate that's definitely not the way to do it). Given that Foo works with the include structure it has, I expect it to work the same way for Bar, and I want to do it the same way for Bar for consistency across the project.

Other info...

Thanks in advance!

EDIT: Adding full error here for clarity...

"D:\src\[PATH1]\[PARENT].vcxproj" (default target) (1) ->
"D:\src\[PATH2]\[PROJECT].vcxproj" (default target) (2) ->
(ClCompile target) -> d:\src\[PATH]\Bar.h(86,1): error C1010:
unexpected end of file while looking for precompiled header. Did you
forget to add '#include "precomp.h"' to your source?
[D:\src\[PATH2]\[PROJECT].vcxproj]

Solution

  • As mentioned in my comments above (but I wasn't sure), this can happen when you add a .h file in such a way that Visual Studio (or MSBuild) tries to compile the .h file directly rather than being compiled when included in a .cpp file.

    To do this "right" the easiest way is to ensure that you are right-clicking the "Header Files" folder/filter in your Solution Explorer in visual studio when you're adding the existing header file, and the "Source Files" folder/filter for compiled (.c, .cpp) files. Note that folders here do not need to reflect actual physical folders (and in many cases do not). They are meant to be logical divisions of files, and often divide on type as well.

    Regardless of the folder though, a file can be misconfigured. To see this with Visual Studio, right-click the file in question. The "Item Type" field should say "C/C++ Header" and not "C/C++ compiler" for a header file. Similarly, a .cpp file should say "C/C++ compiler" and have a lot more options because of a "C/C++" category on the left-hand-side. Note: this is where you can put file-specific compiler commands if needed, and see how the "C/C++->Precompiled Headers" section is different for pch.cpp than all the others in the project!

    If looking at the XML files though, look in PROJECT_NAME.vcxproj and the header files should be inside a tag <ClInclude Include="Foo.h" /> whereas the source files are in <ClCompile Include="Foo.cpp" /> Your ".vcxproj.filters" file should also have similar XML tags. If the header or source files are in the wrong tags there, then you have a problem.

    Hopefully this is helpful to other VS users out there having a similar problem compiling.