In this session,
we will learn about "Precompiled Header"
We need to review what we learned in our previous session.
To recap our previous session,
1. learned the concept of "Compilation"
2. "Linking"
3. "Preprocessing"
4. "Translation unit" means.
cl main.cpp --- compile main.cpp and generate main.obj -- object file
and link main.obj and generate main.exe --- executable
cl /c main.cpp --- "compile" main.cpp and generate main.obj
and do not create executable.
cl main.obj --- "link" main.obj and generate main.exe -- executable
cl /P main.cpp --- "preprocess" main.cpp and generate main.i -- translation unit.
"Translation unit" means the result of preprocessed .cpp file.
While "preprocessing" stage, preprocessor directives are
substituted or replaced with files or macros
We also learned how to define preprocessor macros on the command line
cl /main.cpp /DMAX_INT_VALUE=30
It means we MAX_INT_VALUE 30
In this session,
we will learn /Fo, /Fe, /Fp switches
and /Yc and /Yu switches.
/Fo: filename.obj, /Fe: filename.exe, /Fp: filename.pch
/F stands for file
/Fo: stands for object filename
/Fe: stands for executable filename
/Fp: stands for precompiled header file .pch name
/Yc"header.h" means Yank "header.h" and Create "header.pch"
/Yu"header.h" means Yank "header.h" and Use "header.pch"
/Yc"header.h" /Fp: myheader.pch means regard "header.h"
as a precompiled header file and Create "myheader.pch"
/Yu"header.h" /Fp: myheader.pch means or regard "header.h"
as a precompiled header file and Use "myheader.pch"
cl /EHsc /c /Yc"headers.h" headers.cpp
/Yc"headers.h" means "regard headers.h as the precompiled header file
and generate headers.pch, "
/c headers.cpp means compile headers.cpp and generate headers.obj
========
cl /EHsc /c /Yc"headers.h" headers.cpp
compile (/c) headers.cpp and generate headers.obj file
and Yank (/Yc) headers.h and generate the precompiled header headers.pch
cl /EHsc /c /Yc"headers.h" /Fp"myheaders.pch" headers.cpp
compile (/c) headers.cpp and generate headers.obj file
and Yank (/Yc) headers.h and generate myheaders.pch (/Fp"myheaders.pch")
cl /EHsc /c /Yc"headers.h" /Fp: mypch.pch headers.cpp /Fo: h.obj
compile (/c) headers.cpp and generate h.obj (/Fo: h.obi)
and Yank (/Yc) headers.h and create mypch.pch (/Fp: mypch.pch)
cl /EHsc /Yu"headers.h" /Fpmypch prmain.cpp util.cpp h.obj /Fe: m.exe
/Yu"headers.h" means Yank "headers.h" and use it as the precompiled header file.
regard "headers.h" as the precompiled header file
if the compiler encounter "headers.h"
then substitute it with mypch.pch (/Fpmypch)
=======
cl /EHsc /c /Yc"headers.h" headers.cpp
creates "headers.pch" and headers.obj
cl /EHsc /c /Yc"headers.h" /Fp: mypch.pch headers.cpp
create "mypch.pch" and headers.obj
/Yc"headers.h" means yank headers.h and regard it as a precompiled header
/Fp: mypch.pch means name the precompiled header as mypch.pch
cl /EHsc /c /Yc"headers.h" /Fp: mypch.pch headers.cpp /Fo: hdr.obj
cl /EHsc /Yu"headers.h" /Fp: mypch.pch prmain.cpp util.cpp hdr.obj /Fe: m.exe
/Yu"headers.h" /Fp: mypch.pch
Yank headers.h and when the compiler encounters "headers.h"
subtitute "headers.h" with "mypch.pch"
===========
cl /EHsc /c /Yc"headers.h" headers.cpp
yanks and creates headers.pch and headers.obj
cl /EHsc /Yu"headers.pch" prmain.cpp util.cpp headers.obj /Fe: m.exe
/Yu"headers.pch" means ... headers.pch is the precompiled header file
/Yc and /Yu and /Fp switches means ...
It seems complicated and confusing ... but it is the default option in Visual Studio.
In next session,
We will learn how these switches /Yc /Yu /Fp switches
are used in the Visual Studio IDE
Please refer to MSDN about Precompiled Headers