COMPILING RESOURCES

During program development, resources are defined in a ”resource script,“ which is an ASCII text file with the extension .RC. The resource script can contain ASCII representations of resources and can also reference other files (either ASCII or binary files) that contain resources. The resource compiler (RC.EXE) compiles the resource script into a binary form, adds the resources to the end of the .EXE file that LINK generates, and creates a ”resource table“ in the .EXE header.

You can use the resource compiler included in the Windows Software Development Kit in one of three ways:

You can compile resources and add them to the linked .EXE file in one step by executing the command:

RC filename

where filename.RC is the name of the resource script (the .RC extension is assumed) and filename.EXE is the name of the linked .EXE file. You can also use:

RC resource-name exe-name

if the name of your .RC resource script and the name of your .EXE executable are different. (This is usually not the case.)

You can compile a .RC resource script into a binary compiled form with the extension .RES by executing:

RC -r filename

This uses the ASCII filename.RC file to create a binary file called filename.RES. You can then add the resources to the linked file by executing:

RC filename.RES

The .RES extension is required here to differentiate this command from the command shown earlier that both compiles the resource script and adds the resources to the .EXE file.

If your program has no resources, you should run rc.exe on the linked file:

RC filename.EXE

This flags the program as being ”Windows 3 aware.“

This second method is the one most commonly used when the resource script contains resources. Although it requires that the RC.EXE resource compiler be run twice_once to compile the resource script and again to add the resources to the .EXE file—it actually results in a faster edit-make-run cycle when developing Windows programs. The reason is that compiling the resources generally takes much longer than adding them to the .EXE file. During program development, you will probably modify your C source code much more frequently than the resource script, so you have no need to recompile the resources each time.

The procedure of compiling resources is reflected in a different make file. Up until now we have been using a make file that looks something like this:

progname.exe : progname.obj progname.def

link progname, /align:16, NUL, /nod slibcew libw, progname

rc progname.exe

progname.obj : progname.c

cl -c -Gsw -Ow -W2 -Zp progname.c

When we start using resources, we'll use an expanded make file that looks like this:

progname.exe : progname.obj progname.def progname.res

link progname, /align:16, NUL, /nod slibcew libw, progname

rc progname.res

progname.obj : progname.c [progname.h]

cl -c -Gsw -Ow -W2 -Zp progname.c

progname.res : progname.rc [progname.h] [and other files]

rc -r progname.rc

In the second and third sections I've indicated that a .H header file can be used in both the C source code and the resource script. This header file usually defines identifiers used by the program to reference resources. I've also indicated in the third section that the depen- dent file list possibly includes ”other files.“ These are files referenced from within the resource script. Generally they are binary files that contain icons, cursors, or bitmaps.

The RC.EXE resource compiler uses a preprocessor called RCPP.EXE. This preprocessor folds added or subtracted constants, recognizes /* and */ as comment delimiters, and recognizes the C preprocessor directives #define, #undef, #ifdef, #ifndef, #include, #if, #elif, #else, and #endif. The #include directive works a little differently than in normal C programs. We'll examine this in greater detail in Chapter 10.

In the first section of the make file, the .OBJ and .RES files are dependent files for the .EXE target. NMAKE checks the rest of the make file to determine if these dependent files must be updated. The second section compiles the C source code as usual. The third section compiles the .RC resource script into a binary .RES file.

The first section is then executed if either the .OBJ, .DEF, or .RES file has changed since the last .EXE file was created. This section links the program as usual and runs .RC again to add the resources to the .EXE file. If you change only the .RC resource script file, you still need to relink to produce a new .EXE file without the previous resources. The resource compiler cannot remove old resources from a .EXE file when adding new ones.