4

Is there a way to set Visual Studio solution parameters so it just create precompiled headers without building whole solution. Specifically, it is a huge c++ solution with many projects in itself.

Thank you.

Ajay
  • 18,086
  • 12
  • 59
  • 105
Mujo Osmanovic
  • 931
  • 2
  • 11
  • 15

3 Answers3

11

Here's an article describing the benefits and how to set it up. If you have larger projects, it is definitely worth the few clicks and the extra disk space.

The article says you need to add stdafx.h to all sources and headers; it's not correct. It's sufficient to add to sources - make sure it's the first line though as everything before it will be ignored.

The article does not mention it, but you'll be getting errors from sources that do not include the stdafx.h. You have a choice to resolve this error: you either add it, or exclude the source(s) from the precompilation process. To exclude source files:

  • select source file(s) in the solution explorer; yes you can select more at once,
  • right click on the highlighted source file,
  • click properties from the pop-up menu,
  • select 'All configurations' from the combo-box on top,
  • under C-C++ configuration click 'Precompiled headers',
  • on the right-hand side select 'Not using precompiled headers',
  • click apply,
  • click ok.

Enjoy your new builds in a few seconds from here on (the first build will take longer).

O'Neil
  • 3,790
  • 4
  • 16
  • 30
Csimbi
  • 195
  • 1
  • 3
  • This is a good answer to my question if the pch file needs to be included in the header. Really good point to just add it to the source files. – Xcessity Mar 21 '23 at 11:28
11

Only select the pch creator source file (usually stdafx.cpp), and compile that (Ctrl-F7, or right-click it and select 'Compile')

More info since it doesn't seem to be working for you:

In every project that uses a precompiled header, there is one source file that is used to create the pch file, and the rest only use the pch file. This source file usually only consists of one line:

#include "StdAfx.h"

"Stdafx.h" is the default precompiled header file name in Visual C++, it could be something else for you. If you compile "StdAfx.cpp" by itself, that generates a file with the name "Your_Project_Name.pch" (again, that's only the default). You should see it in the intermediate directory, the same one where all the obj files are. This is the precompiled header. If you did like I said, and selected 'Compile' and not 'Build', then no other files will be compiled, and no linking will take place.

If that still does not solve your problem, then I have no idea what you are asking.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • I don't think this will resolve my problem. I need only to create precompiled headers of all cpp files without doing build or link action. Sorry if I didn't describe my problem properly. Thanks anyway. – Mujo Osmanovic Jul 11 '11 at 15:17
  • If this doesn't solve your problem, then you *still* haven't described your problem properly. The solution I've described here is exactly how you create the precompiled header without building or linking. – Benjamin Lindley Jul 11 '11 at 15:22
  • Thanks a lot for this explanation. In the end it turns out that I actually need preprocessed files and not precompiled headers. Sorry for being stupid. – Mujo Osmanovic Jul 13 '11 at 17:44
  • In the **GCC** world, **.pch** files are called **.gch**. – Patapoom Aug 02 '17 at 13:49
2

If you right-click any Cpp files except stdafx.cpp from your project and set Excluded from build to Yes, it will only generate the precompiled header.

You can achieve the same result through the command line or if you create new project containing only your stdafx.cpp

PeeWee2201
  • 1,464
  • 2
  • 15
  • 23