2

I'm having troubles setting up a project with multiple precompiled headers. As far as I read I can have as many as I want as long as there is only 1 per compile unit. Consider following example of targeted hierarchy:

source
  precomp1.h
  precomp1.cpp (only includes precomp1.h)
  precomp2.h
  precomp2.cpp (only includes precomp2.h)
  main.cpp (includes precomp1.h)
  module1
    fileA.h
    fileA.cpp (includes precomp1.h)
  module2
    fileB.h
    fileB.cpp (includes precomp2.h)

I've setup precomp1&2.cpp as /Yc.

I've setup the project settings as:

PCH: /Yu
PCH File: precomp1.h;precomp2.h
PCH Output: $(IntDir)module1.pch;$(IntDir)module2.pch

Also added $(SolutionDir)\source as additional include, otherwise intellisense gets all weird.

When I try to compile something it gives me error:

"unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"

Any poiners what I'm missing?

Gregor Sattel
  • 352
  • 3
  • 12
  • If you want to compilation with multiple precompiled headers. I suggest you should create a new PCH file for all headers. "Stdafx.h" is a precompiled header. I suggest you check whether the precompiled header file(property->c/c++->Precompiled Headers->precompiled header file) is successfully set. – Jeaninez - MSFT Apr 14 '21 at 03:01
  • @Jeaninez-MSFT What do you mean new pch file for all headers? Shouldn't each header have its own pch file? Also I've included the precompiled header settings in my question - the files are set there. – Gregor Sattel Apr 14 '21 at 04:51
  • @drescherjm I have a single project - this is the hierarchy of a single project I have described. That said given the complications it might just be simple to split each module in separate project... – Gregor Sattel Apr 14 '21 at 04:53
  • According to the doc: [Precompiled Header Files](https://learn.microsoft.com/en-us/cpp/build/creating-precompiled-header-files?view=msvc-160) `Although you can use only one precompiled header (.pch) file per source file, you can use multiple .pch files in a project.` You could set per cpp file which pch to use. You could go to the .cpp file's properties and then go to the pre-compiled header tab. (Configuration Properties -> C/C++ -> Precompiled Headers) You can specify which pre-compiled header to use there. – Jeaninez - MSFT Apr 14 '21 at 06:20
  • @Jeaninez-MSFT Ouch... You are saying I have to specify per cpp which header to use? That seems awfully inconvenient and time-consuming task. I'll give it a try to see if it works. (Although I've tried this approach only for the precompiled headers .cpp files and they still won't compile) – Gregor Sattel Apr 14 '21 at 06:28

0 Answers0