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.

3
  • I don't understand the question Jul 11, 2011 at 13:26
  • Sorry, maybe I have been confusing. I have VS project, I want to run build on that project so VS just creates precompiled headers of all cpp files, but not to build the project. Jul 11, 2011 at 13:30
  • Do you want to compile all of the cpp files to object files, but not perform linking?
    – qid
    Jul 11, 2011 at 17:00

3 Answers 3

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.

4
  • 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. Jul 11, 2011 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. Jul 11, 2011 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. Jul 13, 2011 at 17:44
  • In the GCC world, .pch files are called .gch.
    – Patapoom
    Aug 2, 2017 at 13:49
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).

1
  • 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 at 11:28
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.