10.3.8 Extracting Filename Components

“Special Macros,” Section 10.3.4.3, showed how qualifiers could be added to macros that represented filenames in order to select components of the name or path. This feature is especially useful when creating a general-purpose description block that works with the name of any dependent.

Besides these macro modifiers, NMAKE offers another feature that allows you to extract components of the name of the first dependent file as you have specified it in the description file or on the command line (not the full filename specification on disk). The components can then be recombined with specific paths, extensions, or directories to create the particular name or path you need, without having to specify the exact name or path when you write the description block.

The first dependent file is the first file listed to the right of the colon on a dependency line. If a dependent is implied from an inference rule, NMAKE considers it to be the first dependent file. If more than one dependent is implied from inference rules, the .SUFFIXES list determines which dependent is first.

You can use either of the following syntaxes:

%s

%|[[parts]]F

where parts can be one or more of the following letters, or can be omitted:

Letter Description
No letter Complete name
d Drive
p Path
f File base name
e File extension

You can specify more than one letter. The order of the letters is not significant; NMAKE constructs the filename that meets (or comes closest to meeting) all the specifications. The letters are case sensitive.

The %s option substitutes the complete name; it is equivalent to both %|F and %|dpfeF.

NMAKE interprets any percent symbol (%) within a command line (either in a description block or an inference rule) as the start of a file specifier using this syntax. Therefore, if you need to use a literal percent symbol within a command line, you must specify it as a double percent symbol (%%).

Example

The following example demonstrates this special syntax:

sample.exe : c:\project\sample.obj

LINK %|dpfF, a:%|pfF.exe;

This example represents the following command:

LINK c:\project\sample, a:\project\sample.exe;

In this example, the sequence %|dpfF represents the same drive, path, and base name as the dependent on the dependency line, while the sequence %|pfF represents only the path and base name of the dependent. The command tells the LINK utility to build the executable file on another drive in a directory of the same name.