Search code examples
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...

  • Code project in C++
  • Project uses a precomp.h precompiled header
  • Project contains Foo.h and Foo.cpp
    • Foo.h does not reference precomp.h
    • Foo.cpp starts with #include "precomp.h" and #include "Foo.h"
    • No build issues

Problem...

  • I am working on adding Bar.h and Bar.cpp to add a new functionality set, architecturally similar to what is in Foo.h/.cpp, and in the same directory
  • Mirroring Foo, I do not include precomp.h in Bar.h, and include precomp.h and Bar.h in Bar.cpp
  • I added Bar.h and Bar.cpp to the .vcxproj file the same way that Foo.h and Foo.cpp are added
  • The same build type that passes before my addition of Bar fails once I add Bar.h/.cpp with the error in the title (error C1010: unexpected end of file while looking for precompiled header)

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...

  • I'm doing a very thorough clean/rebuild every time, so any weird state issues should be able to be eliminated as the cause
  • Both Foo.h and Bar.h include #pragma once at the top, FWIW
  • This project is already set up to use precompiled headers, so presumably whatever is the issue should be specific to the files I'm adding (and the error is specifically referencing Bar.h)

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.