Search code examples
c++visual-studiovisual-c++precompiled-headers

what is the most unobtrusive way of using precompiled headers in Visual C++?


Say I have a single project, with files A.cpp, B.cpp, C.ppp and matching header files (and that's it). The C++ files include system headers or headers from other modules.

I want to compile them to a library with command line actions (e.g., using Make), using 'cl', with the precompiled headers feature.

What are the steps I should do? What are the command line switches?


Solution

  • Precompiled Headers are done by creating a .cpp that includes the headers you want to be pre-compiled. Normally, stdafx.cpp is used for this purpose.

    1. Create a .cpp that includes the headers you want to be precompiled -- normally this file would be called stdafx.cpp
    2. Add the .cpp to your project
    3. Compile that file with /Yc before any other .cpp files /Fp sets the name of the .pch file with the pre-compiled information
    4. Compile all other files with /Yu and /Fp with the name of the .pch from #3

    There are other ways, but I find this to be the simplest.