• No results found

You operate CPP the same as the BCC command-line compiler. You enter the same command-line switches and filename specifications. The default mode of operation processes the input files and produces an output text file where each line is prefaced with both the source filename and a line number. The output is written to a file whose name is constructed from the module or project name and which ends in an .i extension. Preprocessing a program named begin.c produces a file named begin.i.

Optionally, you may disable the source and line number information by adding the -P command-line switch when you invoke CPP. The resulting output text file then will contain only preprocessed program source. If you want to, you can run the resulting text file (only when -P is used to strip the line numbers) through the BCC compiler to produce an executable program. When you look through the output file, you will notice that there are no comments, because they are removed by the preprocessor. What might look especially unusual is the presence of many blank lines. These blank lines occur when large block comments are deleted or macro definitions and conditional statements are evaluated and removed.

Listing 3.1 shows a sample program named democpp.c. Listing 3.2 shows democpp.i, the output file produced after preprocessing democpp.c using CPP.

Note the expansion of the #include statement, and note also the elimination of the manifest constant macros, the macro sumit, and the conditional compila-tion directives. In Listing 3.2, large blocks of blank lines and porcompila-tions of the stdio.h expansion have been removed in order to save space.

LISTING 3.1. DEMOCPP.CBEFOREPREPROCESSINGWITH CPP.

1 /* DEMOCPP.C

2 Demonstrates the usage of the CPP preprocessor.

3 */

4 #include <stdio.h>

5

6 #define prompt1 “Good, Morning”

7 #define prompt2 “Good, Afternoon”

8

9 /* Use this for conditional compilation */

10 #define useprompt1 1 11

12 /* Illustrate macro expansion */

13 #define sumit(a,b,c) ( (a) + (b) + (c) ) 14

15 void main( void ) 16 {

17 #ifdef useprompt1

18 printf(“%s\n”, prompt1);

19 #else

20 printf(“%s\n”, prompt2);

21 #endif 22

23 printf(“The sum of 7 + 9 + 11 is: %d\n”, sumit(7,9,11) );

24 25 }

LISTING 3.2. DEMOCPP.ISHOWINGTHEEFFECTSOFPREPROCESSING DEMOCPP.CUSING CPP.

[Blank lines deleted for clarity.]

C:\BC3\INCLUDE\_defs.h 1:

C:\BC3\INCLUDE\_defs.h 2:

C:\BC3\INCLUDE\_defs.h 3:

[Blank lines deleted for clarity.]

C:\BC3\INCLUDE\_defs.h 105:

[Blank lines deleted for clarity.]

C:\BC3\INCLUDE\_nfile.h 16:

[Blank lines deleted for clarity.]

C:\BC3\INCLUDE\_null.h 15:

C:\BC3\INCLUDE\stdio.h 26: typedef unsigned size_t;

C:\BC3\INCLUDE\stdio.h 27:

C:\BC3\INCLUDE\stdio.h 28:

C:\BC3\INCLUDE\stdio.h 29:

C:\BC3\INCLUDE\stdio.h 30:

C:\BC3\INCLUDE\stdio.h 31: typedef long fpos_t;

C:\BC3\INCLUDE\stdio.h 32:

C:\BC3\INCLUDE\stdio.h 33:

C:\BC3\INCLUDE\stdio.h 34:

C:\BC3\INCLUDE\stdio.h 35:

C:\BC3\INCLUDE\stdio.h 36: typedef struct { C:\BC3\INCLUDE\stdio.h 37: int level;

C:\BC3\INCLUDE\stdio.h 38: unsigned flags;

C:\BC3\INCLUDE\stdio.h 39: char fd;

C:\BC3\INCLUDE\stdio.h 40: unsigned char hold;

LISTING 3.2. CONTINUED C:\BC3\INCLUDE\stdio.h 41: int bsize;

C:\BC3\INCLUDE\stdio.h 42: unsigned char *buffer;

C:\BC3\INCLUDE\stdio.h 43: unsigned char *curp;

C:\BC3\INCLUDE\stdio.h 44: unsigned istemp;

C:\BC3\INCLUDE\stdio.h 45: short token;

C:\BC3\INCLUDE\stdio.h 46: } FILE;

[Blank lines deleted for clarity.]

C:\BC3\INCLUDE\stdio.h 47:

C:\BC3\INCLUDE\stdio.h 48:

[Blank lines deleted for clarity.]

C:\BC3\INCLUDE\stdio.h 106:

C:\BC3\INCLUDE\stdio.h 107: extern FILE _ _cdecl _streams[];

C:\BC3\INCLUDE\stdio.h 108: extern unsigned _ _cdecl _nfile;

C:\BC3\INCLUDE\stdio.h 109:

C:\BC3\INCLUDE\stdio.h 110:

[Approximately 240 lines deleted here, including nested includes and other definitions brought in by stdio.h.]

C:\BC3\INCLUDE\stdio.h 249:

democpp.c 15: void main( void ) democpp.c 16: {

democpp.c 17:

democpp.c 18: printf(“%s\n”, “Good, Morning”);

democpp.c 19:

MAKE

The IDE includes a built-in project make facility that automates the task of keeping track of which modules need recompiling. But when you use the stand-alone command-line compiler BCC, you must use MAKE to access the project make facilities. MAKE is a stand-alone utility for which you must prepare a separate make file describing the dependencies between the files that cause them to be recompiled or reassembled. MAKE is provided in the Borland C++

package and installed in the \borlandc\bin directory. MAKE is a protected-mode application. Borland also provides MAKER.EXE, which is identical to MAKE except that it runs in real mode only. If you can, use the protected-mode version of MAKE, because it can process much larger projects than MAKER.

As soon as a make file is created to describe the requirements of your application, MAKE uses the make file to check and compare the date and time stamps assigned by DOS to each file. If any source file is newer than its corresponding object file, MAKE ensures that each object file is recompiled or reassembled to incorporate the latest changes. The make file describes the dependencies between the source files and the object modules, and specifies what commands should be issued in order to recompile or reassemble the source or to access the resource compiler.

If you are using BCC and currently are recompiling all your source files each time you make simple changes, you can save yourself a great deal of time by learning how to use MAKE. MAKE is not difficult to use, especially for building fairly straightforward projects. MAKE does, however, include a large variety of options and capabilities. Fortunately, you can ignore most of these options as you prepare to create your first make files. As you’ll see in this section, learning to use MAKE’s basic functionality is quite straightforward— only the advanced features get a bit messy. If you want to, you can convert project files created within the IDE into make command files (see the section “PRJ2MAK” later in this chapter).