1

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]
  • Doesn't msvc tell you the header to include at the end of the error message in the Output Tab? For me when I forget it tells me Did you forget to `#include mypch.h"` and then I put this exact line as the first line of the .cpp file and all is well. – drescherjm Jun 06 '23 at 16:32
  • @drescherjm yeah it tells me I forgot precomp.h, which is already included at the top of Bar.cpp. EDIT: Added full error to original post. – overcaffeinated Jun 06 '23 at 16:41
  • Did you verify that you only have a single `precomp.h` and not more than 1. – drescherjm Jun 06 '23 at 16:47
  • ***(ClCompile target) -> d:\src\[PATH]\Bar.h(86,1): error C1010:*** its odd that is complaining about the header and not the cpp file. Also this does not look like the typical msvc build output. I am a little confused at it. – drescherjm Jun 06 '23 at 16:48
  • I wonder if the .h file was added to the project incorrectly, so it's flagged to attempt to compile it, rather than ignore it. That would probably cause this error. – Kevin Anderson Jun 06 '23 at 16:52
  • 2
    Do not add Bar.h to the "Source Files" folder of the project. Only .cpp files go there, the ones that actually need to be compiled. You can add it to the "Header Files" folder, but that's entirely optional. – Hans Passant Jun 06 '23 at 16:56
  • @drescherjm yes there is only one precomp.h – overcaffeinated Jun 06 '23 at 17:08
  • @drescherjm I'm using msbuild from cmd, and stuff like [THIS] is me truncating the paths and such. – overcaffeinated Jun 06 '23 at 17:09
  • @KevinAnderson suggestions on how to check that the .h was added incorrectly? – overcaffeinated Jun 06 '23 at 17:10
  • @HansPassant This project (or this module at least) is not using separate source files and header files folders, source and header differentiation is done via the vcxproj file. – overcaffeinated Jun 06 '23 at 17:11
  • @overcaffeinated - Two ways. In visual studio, right-click the file and ensure that the "Item Type" field is "C/C++ Header" and **not** "C/C++ compiler". As well in your ".vcxproj" file, the headers will be in an XML tag of `` whereas the cpp files should be in `` Your ".vcxproj.filters" file should also have similar XML tags. – Kevin Anderson Jun 06 '23 at 17:14
  • Also @HansPassant was suggesting the folders in visual studio's view, not necessarily actual physical folders (they're called "filters" sometimes in visual studio). So if you added the files to the wrong "folder" in visual studio, it could cause the problem you're seeing, maybe. – Kevin Anderson Jun 06 '23 at 17:15
  • Ah, yep, that was it @KevinAnderson, thanks! I had Bar.h in the correct section of the XML, but I had it as ClCompile instead of ClInclude. In retrospect, should have looked at the error message more closely. – overcaffeinated Jun 06 '23 at 17:38
  • 1
    @KevinAnderson you want to write that up as an answer and I can mark that as the solution? I could do it, but figure you should get the points since you caught it. – overcaffeinated Jun 06 '23 at 17:39
  • @overcaffeinated - Answer posted. And no worries, guess how I learned to look for this problem? `**looks fake innocent**` - aka: I've done this. It happens. – Kevin Anderson Jun 06 '23 at 17:57
  • @overcaffeinated ***I'm using msbuild from cmd*** Ahh that explains it, Thanks. I use CMake with msvc since 2008 at work. So for me I type something like `cmake --build . --config Release` for a command line build or run similar from a script. – drescherjm Jun 07 '23 at 13:24

1 Answers1

1

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.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54