Advanced Preprocessor Statements

Chapter 3 - Writing, Compiling, and Debugging Simple Programs

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Creating the Executable File
Most Windows 3.x, Windows 98/95, and Windows NT programs contain many files. Initially, however, most simple C/C++ programs start with just one file, the main( ) C/C++ file. As you become a more experienced programmer, this introductory approach will prove to be inefficient.
As your understanding of C/C++ and Windows application development increases, you will begin to break your solutions into multiple, logically related C/C++ files. To these you will add your own header files (header files have an h file extension). By the time you reach the end of this book, you will be creating applications with source code files, header files, resource files, and so on.
So, even though the sample program contains just a single file, the following sections explain the steps necessary to build a fully formed C/C++ application under Windows.
Using Workspaces
Before you can compile a typical C/C++ program, the Visual C++ IDE needs to be informed of the names of all of the C/C++ and resource files needed to create the executable file. In the past, this process was typically done in a make file (make files have a .MAK file extension).
Make files are text files that follow a special syntax that details file dependencies. In other words, the syntax of the make file defines the files that must be present and compiled before the target file can be used in another phase of the compile or link process. By the way, due to the sophistication of this process, it is no longer called “compiling and linking.” The word now used to describe these steps is “building.”
Traditionally, make files had to be executed from the command line by a stand-alone utility program known as NMAKE. Microsoft has streamlined this entire process in the Visual C++ compiler by including a substitute utility called the Project utility. This utility can be accessed from within the IDE.
Whereas a programmer previously had to create a separate *.MAK text file and run the NMAKE utility, the Project utility now allows you to achieve the same result without having to quit the Visual C++ IDE. The Project utility creates, edits, and uses make files with a *.MAK file extension. What’s even better is that creating the project file is an easy process.
Starting a New Project
To create a new project, choose the File | New... menu item. This command opens up the New File dialog box. This time select the Projects tab (see Figure 3-4). After you have selected this item, you will see the New Project dialog box, which is the first step in creating a project file.
Figure 3-4: The New Projects dialog box is used to create a new project file
The first piece of information that the Project utility requires is a name for the project file. The name of your project file is important, since this is the label that will be used to name the final executable file.
Many first-time C/C++ programmers are surprised that the name of the program’s executable file does not match the name of the source file containing the main( ) or WinMain( ) function. Remember, all project files must have a *.MAK file extension, but their actual name may be different from your source code files. For our sample program, use the project file name ERROR.MAK.
The second piece of information required is the project type. Options include dynamic link libraries (DLLs) and various executable formats. For this example, the Win32 Console Application option should be selected.
If you place an entry in the Location: category, this option instructs the Visual C++ IDE to automatically create a new subdirectory for your new project.
A fourth option involves the project’s target platform. For the 32-bit version of the Visual C++ compiler, the Win32 option is active. Figure 3-3, shown earlier, holds information for a completed sample project file. To accept this information, click on the OK button.
Adding Files to a Project
Once a new project file is defined, the Project utility allows you to easily add files. Figure 3-5 shows the Add Files to Project... menu item highlighted. Accessing this pop-up menu is as simple as clicking the right mouse button within the FileView.
Figure 3-5: The Add Files to Project menu item
Selecting this option opens a standard File Manager window, allowing you to easily locate and then include all of the files necessary to create the executable program. One note about the types of included files: Header files (files with .H file extensions) are not inserted into a project’s file list. Header files are incorporated directly into the build process by #include preprocessor statements.
The Insert Files into Project dialog box (see Figure 3-6) is very similar to the standard Windows File dialog box. It allows you to select a default drive and path, and it automatically lists the target path’s filenames. For our sample program, simply double-click on the ERROR.C filename in the File name: list. This will automatically insert the filename into the project file.
Figure 3-6: Adding the file ERROR.C to the project
If this were a more fully formed project file, such as one that would be used for Windows application development, at this point you would continue to select files needed by the project. For our sample program, however, one file will do. At this point you are ready to formally end the project file’s definition by clicking on the OK button.
Choosing Build or Rebuild All
Now that the project file has been created, you are ready to instruct the IDE to create the executable file. Remember, under Visual C++ this process is called a build.
Figure 3-7 shows the Build menu with the Rebuild All command highlighted.
Figure 3-7: The Rebuild All menu item will compile and link this new application
In Chapter 2, you learned that the only difference between the Build and Rebuild All commands is that Rebuild All does not check the dates of any of the files used by the project. This command always recompiles and links every file in the project.
Because a poorly maintained system can have inaccurate internal clock settings, it is always safest to choose the Rebuild All option for small applications. Now, activate the build process by clicking on the Rebuild All command or pressing enter when the command is highlighted.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net