Edit: I know how to use it in VS, and CMake, I am trying to find out how to do it using cl.exe on the command line and not from inside the Visual Studio IDE or using a build generator like CMake.
Original question:
I am trying to use precompiled headers on windows with MSVC. mainly to bundle all Microsoft’s headers in one place and precompile them for faster compile builds.
The problem is I can not figure out how to actually use pch files.
I was trying following the two links from the official documentation:
Create Precompiled Headers
and also this:
Use Precompiled Headers
I am generating the .pch file are follows:
cl /c /Ycheaders.h main.cpp
inside headers.h I do the following:
#include <windows.h>
#include <iostream>
//--- other includes that will never change ----//
And following the documentation this is how I try to use it.
cl /Yuheaders.h main.cpp
this get me the following error:
fatal error C1854: cannot overwrite information formed during creation of the precompiled header in object main.obj
I assumed the documentation is wrong (because the provided command in the docs: cl /Ycheader.h main.cpp is wrong and does not work and it needs /c before it as in the comment in the example code), It did not make sense to me to compile your main.cpp so I repeated the steps above with creating the precompiled headers files with headers.cpp instead of main.cpp where headers.cpp is just an empty source file with a single include headers.h and the I get a linking error 1 unresolved external LINK error 1120 without specifying what was unresolved.
Ideally if anyone has a minimal example of how to create header files for msvc and then shows how to use them.
2
Answers
Create pch:
Use pch:
Assuming you already have a project without precompiled headers.
Start by adding two new files:
precomp.h
andprecomp.cpp
:precomp.cpp
is simply this:precomp.h
is the header file with all the stuff you want precompiled – typically system and CRT header files:Update every non-header source files (that is, the .cpp source files) files in the project to have
#include "precomp.h"
as the first line before any other code.Now, right click on the project title in the solution explorer:
And select "Properties" to bring up the project settings for the entire project:
Under C/C++ section of the project properties. Select "precompiled headers". Then enable the Precompiled Header option to be
Use (/Yu)
. Set the Precompiled Header File option toprecomp.h
. Then click OK.Final step. Right click directly on
precomp.cpp
in your Solution Explorer and select "Properties".This will display up a dialog similar to one you just saw for the project settings, but specific to this source file.
For this file’s precompiled header options, select
Create (/Yc)
and the Precompiled Header option andprecomp.h
as the Header file option.Now do a "clean" build followed by a full build. Precomp.cpp will get compiled first and generate the needed
.pch
file. All the other source files upon getting compiled will pull in the precompiled header file.