/D (Define Constants and Macros)

Option

/Didentifier[[=|# [[{string|number}]] ]]

Use the /D option to define constants or macros for your source file.

The identifier is the name of the constant or macro. It can be defined as a string or as a number. No space can separate /D and the identifier. Enclose the string in quotes if it includes spaces.

Note that with Microsoft C/C++, you can substitute a number sign (#) for an equal sign (=) when setting the identifier to string|number. From either the DOS command line or from a batch file, you cannot set an environment variable, such as CL, to a string that contains an equal sign. Environment variables do, however, accept the number sign. Now that the CL driver allows the substitution of “#” for “=,” you can use the CL environment variable to define preprocessor constants:

SET CL="/DTEST#0"

If you omit both the equal sign and the string or number, the identifier is assumed to be defined, and its value is set to 1. For example, entering /DSET defines a macro named SET with a value of 1. Note that the identifier argument is case sensitive. For example, the preceding /D option would have no effect on a constant named set that is defined in the source file.

Use the /D option in combination with either the #if or #ifdef directive to compile source files conditionally.

You can replace a keyword, identifier, or a numeric constant with no text in a source file. To do so, use the /D option with a keyword, identifier, or a numeric constant and append an equal sign followed by a space. For example, Use the following command to remove all occurrences of RELEASE from TEST.C:

CL /DRELEASE= TEST.C

Similarly, use the following command to remove all occurrences of the keyword __far in TEST.C:

CL /D__far= TEST.C

Defining macros and constants with the /D option has the same effect as using a #define preprocessor directive at the beginning of your source file. The identifier is defined until either an #undef directive in the source file removes the definition or until the compiler reaches the end of the file.

If an identifier defined in a /D option is also defined within the source file, CL uses the definition on the command line until it encounters the redefinition of the identifier in the source file.

Example

#if !defined(RELEASE)

__nheapchk();

#endif

This code fragment calls a function to check the near heap unless the constant RELEASE is defined. While developing the program, you can leave RELEASE undefined and perform heap checking to find bugs. Assuming the program name is BIG.C, you would compile with the following command:

CL BIG.C

After you have found all of the bugs in the program, you can define RELEASE in a /D option so the program runs faster, as follows:

CL /DRELEASE BIG.C