Using precompiled headers in msvc when building from the command line with the cl compiler

160 Views Asked by At

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.

3

There are 3 best solutions below

2
Minxin Yu - MSFT On BEST ANSWER

Create pch:

cl /EHsc /c /Ycheader.h header.cpp

Use pch:

cl /EHsc /Yuheader.h /Fpheader.pch main.cpp /link header.obj
1
selbie On

Assuming you already have a project without precompiled headers.

enter image description here

Start by adding two new files: precomp.h and precomp.cpp:

enter image description here

precomp.cpp is simply this:

#include precomp.h

precomp.h is the header file with all the stuff you want precompiled - typically system and CRT header files:

#ifndef MY_PRECOMP_H
#define MY_PRECOMP_H

#include <windows.h>
#include <vector>
#include <map>
// etc...

#endif

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.

enter image description here 

Now, right click on the project title in the solution explorer:

enter image description here

And select "Properties" to bring up the project settings for the entire project:

enter image description here

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 to precomp.h. Then click OK.

enter image description here

Final step. Right click directly on precomp.cpp in your Solution Explorer and select "Properties".

enter image description here

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 and precomp.h as the Header file option.

enter image description here

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.

0
Serilena On

The solutions posted by Elijay and Minxin both work, however, in my case (and I am not entirely sure why) I had to reinstall the VS completely, and disabling the antivirus (I was getting strange threat warnings during compilation/running programs)

After that both of their solutions seems to produce a precompiled header that works