Amir-Mousavi
Amir-Mousavi

Reputation: 4561

changing from `stdafx.h` to `pch.h`

I clone a repo, c++ solution with 3 projects, a console application .exe a dll project .dll and a unit test .dll. I am not sure with what version of VS or compiler it was built or created.

building the solution on my machine was throwing erro

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

I created pch.h and pch.cpp according to another solution in every project.

changed the properties->c/c++->precompiled headers of every project to

cleared solution and deleted every thing in Debug folders

now building the solution throws

fatal error C1010: unexpected end of file while looking for precompiled header. Did you forge t to add '#include "stdafx.h"' to your source?

How should I force all projects in a solution to use stdafx.h or pch.h

Upvotes: 2

Views: 4160

Answers (2)

Wormer
Wormer

Reputation: 735

There is an option in project Configuration properties -> C/C++ -> Advanced -> Forced Include File. Put the new name pch.h here for all configurations under all platforms. This will force the compiler to automatically #include "pch.h" in each and every compilation unit.

Stop here for a moment. For all configurations and all platforms, or the one you're building. Triple-check here, can't say that more. Also, check that you're editing setting of the whole project, but not a single file.

This is useful, because the #include "pch.h" must literarily be the first include in every compilation unit. It won't help if foo.cpp does #include "foo.h" that in it's favor does #include "pch.h" in the first line. Ain't working. File foo.cpp must do #include "pch.h" on it's own, and only then #include "foo.h".

Alternatively if you already renamed stdafx.h into something else everywhere and can't understand where the error C1083: Cannot open include file: 'StdAfx.h': No such file or directory is coming from Forced Include File is the place to check!

Upvotes: 0

Sami Sallinen
Sami Sallinen

Reputation: 3496

The precompiled headers are a feature to speed up the build process, so if you have trouble building, you can always turn off the precompiled headers (select 'Not using precompiled headers' in the project settings for the whole project) and the build should work.

If you want to make the project build with precompiled headers, you should have the 'Create' option on for only one source file (pch.cpp or stdafx.cpp) that only contains the include statement for the precompiled header file. All the other source files should have the 'use' precompiled headers selection on. Also they must have the correct #include statement for your precompiled header as the 1st non-comment line in the file.

The idea is that the compiler makes a dump of its internal state when processing the precompiled header #include statement in the file with the 'create' option set. It then loads that dump when it sees the corresponding include in the files with the 'use' option turned on. Loading the dump can be much faster than e.g. parsing through windows.h. So yo should put any commonly used large header file #include statements in the pch.h or stdafx.h.

The error messages you got are produced if the .cpp file has the 'use' option on, but the #include statement with the file name set in the 'precompiled header file' option is not found.

Upvotes: 3

Related Questions