views:

138

answers:

3

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?

A: 

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.

Lou Franco
Don't you create a stdafx.h file, and include the headers to precompile in it? And then you need to modify the source files to include stdafx.h as their first include (or use /FI to 'include it from outside')...
Xavier Nodet
A: 

Since you are using visual studio I would recommend just building the solution:

devenv solutionfile.sln /build [ solutionconfig ] [ /project projectnameorfile [ /projectconfig name ] ]

If you really need to do each of the individual files you can look at the command line option at the bottom of the build properties settings for each file in your project. It will show all the switches that you need.

Regarding the PCH, all you need to do is make sure the cpp that is creating the pch is compiled first.

Dolphin
why is this downvoted?
jalf
+1  A: 
  • Create a file stdafx.h, that includes the headers you want to pre-compile.
  • Create a file stdafx.cpp that only includes stdafx.h.
  • Make sure stdafx.pch depends on stdafx.{h,cpp}
  • stdafx.cpp should be compiled with flag /Yc
  • All the other source files should depend on stdafx.pch (so that the latter is created first)
  • All the other source files should have flags '/FI stdafx.h /Yu' ('do as if #include "stdafx.h" was added at the top of the file, and use the PCH file').
Xavier Nodet