The code will use the C++ Standard Library to input and output via streams, so it will use the <iostream> header. The code will use the C++ string type to handle input, so it will use the <string> header. Finally, it accesses the C runtime time and date functions, so the code will use the <ctime> header. These are all standard headers that will not change while you develop the project, so they are good candidates for precompiling.
In Visual Studio create a C++ header file and add the following lines:
#include <iostream>
#include <string>
#include <ctime>
Save the file as utils.h.
Now create a C++ source file and add a single line to include the header file you just created:
#include ″utils.h″
Save this as utils.cpp. You will need to create a makefile for the project, so in the New File dialog, select Text File as your file type. Add the following rules for building the precompiled header:
utils.pch utils.obj :: utils.cpp utils.h
cl /EHsc /c utils.cpp /Ycutils.h
Save this file as makefile. with the appended period. Since you created this file as a text file, Visual Studio will normally automatically give it an extension of txt, but since we want no extension, you need to add the period to indicate no extension. The first line says that the two files, utils.pch and utils.obj, depend on the source file and header file being specified. The second line (prefixed with a tab) tells the compiler to compile the C++ file, not to call the linker, and it tells the compiler to save the precompiled code included into utils.h. The command will create utils.pch and utils.obj, the two targets specified.
When the make utility sees that there are two targets, the default action (when a single colon is used between targets and dependencies) is to call the command once for each target (there are macros that you can use to determine which target is being built). This would mean that the same compiler command would be called twice. We do not want this behavior, because both targets are created with a single call to the command. The double colon, ::, is a work around: it tells nmake not to use the behavior of calling the command for each target. The result is that, when the make utility has called the command once, to make utils.pch, it then tries to make utils.obj but sees that it has already been made, and so realizes that it does not need to call the command again.
Now test this out. At the command line, in the folder that contains your project, type nmake.
If you do not give the name of a makefile, the program maintenance tool will automatically use a file called makefile (if you want to use a makefile with another name, use the /f switch to provide the name):
C:\Beginning_C++\Chapter_01\Code>nmake
Microsoft (R) Program Maintenance Utility Version 14.00.24210.0
Copyright (C) Microsoft Corporation. All rights reserved.
cl /EHsc /c utils.cpp /Ycutils.h
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
utils.cpp
Do a directory listing to confirm that utils.pch and utils.obj have been made.