20.3.3 Creating the Makefile

The NMAKE utility controls the creation of executable files to ensure that only the minimum required processing is performed. Four utilities are used in creating a dynamic-link library:

Microsoft C Optimizing Compiler (CL)

Microsoft Segmented Executable Linker (LINK)

Microsoft Import Library Manager (IMPLIB)

Microsoft Windows Resource Compiler (RC)

A fifth (optional) utility, the Microsoft Symbol File Generator (MAPSYM), is also used but only with the debugging version of Windows.

The makefile for creating the sample library is as follows.

mindll.obj: mindll.c
    cl /ASw /c /Gsw /Os /W3 mindll.c




mindll.dll: mindll.obj
    link mindll.obj libentry.obj, mindll.dll,mindll.map/map, \
        mdllcew.lib libw.lib/noe/nod,mindll.def
    mapsym mindll.map
    implib mindll.lib mindll.def
    rc mindll.dll

For more information about NMAKE see the CL documentation.

20.3.3.1 Compiler Options

CL uses five sets of options, which are briefly described following. For more information, see the CL documentation. The following example shows the options used to compile the sample dynamic-link library:

cl /ASw /c /Gsw /Os /W3 mindll.c

The /ASw option controls the default addressing to be created by the compiler. The S option specifies the small model, which uses short data pointers and near code pointers. The w option tells the compiler that the stack is not part of the default data segment (that is, SS != DS). This causes the compiler to generate an error message when it detects the improper creation of a near pointer to an automatic variable.

The /c option requests compile-only operation. This is required if your dynamic-link library has multiple C-language source-code modules.

The /Gsw option consists of two parts. The s option disables normal CL stack checking. This is required because the stack checking is incompatible with Windows. The w option requests that Windows prolog and epilog code be attached to every FAR function. This code is used for two purposes: to assist in establishing the correct data segment, and to allow the memory manager to move code segments at any time during system operation.

The /Os option tells CL to optimize for size rather than for speed. This option is not required, but is recommended.

The /W3 option sets the warning level to 3 (the highest warning level is 4). It is a good idea to use this option during the development process to allow CL to perform various checks on data types and function prototypes, among others. This option is not required, but it is recommended.

20.3.3.2 Linker Command Line

The link command takes five arguments, each separated by a comma:

link mindll.obj libentry.obj, mindll.dll,mindll.map/map,
    mdllcew.lib libw.lib/noe/nod,mindll.def

The first argument lists the object (.OBJ) files that are to be used to create the dynamic-link library. If you use the standard dynamic-link initialization function, include the LIBENTRY.OBJ file as an object.

The second argument specifies the name of the final executable file. The linker uses the .DLL extension for dynamic-link libraries. Implicitly loaded libraries must be named with the .DLL extension. An implicitly loaded library is imported in the application's module-definition file rather than explicitly loaded by the LoadLibrary function. For more information about loading a dynamic-link library, see Section 20.4, “Application Access to Dynamic-Link Code.”

The third argument is the name of the .MAP file, which is created when you specify the /map option. This file contains symbol information for the global variables and functions. It is used as input to MAPSYM, described in the following section.

The fourth argument lists the import libraries and the static-link libraries required to create the dynamic-link library. There are two listed in this example: MDLLCEW.LIB and LIBW.LIB. MDLLCEW.LIB is a C run-time library that contains some dynamic-link startup code and C run-time library functions and math support. LIBW.LIB contains import information for the Windows API functions. The fourth argument also includes two linker options, /nod and /noe. The /nod option disables default library searches based on memory-model selection. If you use C run-time functions, you must also include the appropriate C run-time library in this library list. The /noe option disables extended library searches. This inhibits the error messages created by the linker when a symbol is identified in multiple libraries.

The fifth argument is the name of the module-definition file, described in Section 20.3.2, “Creating the Module-Definition File.”

20.3.3.3 Symbol File Generator: MAPSYM

MAPSYM reads the .MAP file created by the linker and creates a symbol file having the .SYM extension. The symbol file is used by the debugging version of Windows to create stack trace information when a fatal error occurs.

20.3.3.4 Import Library Manager: IMPLIB

IMPLIB creates an import library with the .LIB extension from a dynamic-link library's module-definition file. An import library is listed on the linker command line of applications that will use the functions in the library. Because of this, references to library functions in an application can be properly resolved.

20.3.3.5 Resource Compiler: RC

All dynamic-link libraries must be compiled by using RC, to mark them as compatible with Windows version 3.1. You can compile the library by using the RC option /p. This marks the library as private to the calling application and means that no other applications should attempt to use the library.