digitalmars.com                        
Last update Mon Mar 23 20:32:52 2009
Compiler & Tools Guide

Tools
BCC
CHMOD
CL
COFF2OMF
COFFIMPLIB
DMC
DIFF
DIFFDIR
DUMP
DUMPOBJ
DUMPEXE
EXE2BIN
FLPYIMG
GREP
HC
IMPLIB
LIB
LIBUNRES
MAKE
MAKEDEP
ME
OBJ2ASM
PATCHOBJ
RC
RCC
SC
SHELL
SMAKE
TOUCH
UNMANGLE
WHEREIS


Compiling
Compiling Code
C Implementation
C++ Implementation
Language Extensions
Mixing Languages
Assembly Language
Inline Assembler
Optimizing Code
Numerics Programming
Regular Expressions
Acrtused
Pragmas
Precompiled Headers
Predefined Macros
Warning Messages
Error Messages
Runtime Messages


Linking
Optlink
Switches
Module Definition Files
Operation and Design
Error Messages


Win32 Programming
Win32 Programming


DOS and Win16
Programming

Memory Models
16 Bit Pointer Types
and Type Modifiers

Handle Pointers
DOS
DOS 32 (DOSX)
Win16
Win16 DLLs
Win16 Prolog/Epilog
Virtual Memory
For 640Kb DOS



C/C++ Extensions
Contract Programming
__debug statement
__debug declaration
Dynamic Profiling
Embedding C in HTML


Porting to DMC++
Switching to DMC++
from Microsoft
from Borland
Porting Guide




OPTLINK

OPTLINK, the Digital Mars linker, links object files and libraries to produce an executable program or DLL for DOS, Windows 3.x, or Win32. For simple programs, the link step can be run automatically by the compiler. For more complex applications that need close control over how the program is linked, run OPTLINK as a separate step.

To control running the linker from the IDDE, set options in the Linker subpage on the Build page of the IDDE's Project Settings dialog box.

Overview

OPTLINK is a super-fast, high-capacity program linker. It is designed for use by professional programmers to efficiently create both standard .exe and .com executable files for use with MS-DOS and segmented .exe files (segmented executable) and dynamic link libraries (.dll) for use with Windows and Win32 in significantly less time than is required by other linkers.

OPTLINK derives its spectacular speed from being written entirely in hand- tuned assembly language, making maximal use of multithreading and processor scheduling. It does full links faster than more pedestrian linkers do incremental links.

OPTLINK combines object (.obj) and library (.lib) files into a single file that is executable by the operating system. OPTLINK can optionally create memory maps and symbol tables that are useful for debugging.

OPTLINK works with modules in Intel OMF format (.obj file), including those generated by standard assemblers, compilers and standard object module libraries. The output files produced have all address assignments resolved and are in one of these standard formats:

  • 16 bit .com/.sys/.bin memory image format (MSDOS)
  • 16 bit DOS .exe relocatable executable MZ format (MSDOS)
  • 16 bit segmented .exe NE format (Windows 3.x)
  • 32 bit .exe dos extender format (DOSX)
  • 32 bit .exe PE format (Win32)
All available memory is used by OPTLINK when linking executable programs. For 16-bit programs, the number of program description objects that can be accommodated is limited to 64KB for each kind of object (PUBLIC, LIBRARY, SEGMENT, and so on). If memory space is inadequate, the program descriptions are automatically paged out to disk files which are then deleted once the link process completes.

These features combine to create an environment in which you can create large programs, despite apparent memory constraints.

Features

Although compatibility with existing linkers was a prime objective in the design of OPTLINK, the product sets itself apart from other linkers with a number of exclusive features. OPTLINK features include:
  • Speed
    Executes faster than any other method for creating executable files. Removal of duplicate records (CVPACK) is built in.
  • Capacity
    Successfully builds huge applications, including debug information.
  • Smart linking
    Unlike others, recursively eliminates unreferenced code. Also eliminates unreferenced uninitialized global variables, see /PACKFUNCTIONS.
  • Efficient program generation options
    Builds smaller, more efficient, faster loading and even faster running 16-bit programs by packing code and data, plus converting intra-segment far calls to near calls, see /PACKCODE, /PACKDATA, /EXEPACK, /FARCALLTRANSLATION.
  • Cross referencing
    Optionally included within map output. Every global symbol and the module name( s) of where defined and referenced is listed, see /XREF.
  • Environment use
    Dedicated environment variable usage simplifies searches.
  • Virtual memory system
    When DOS hosted, automatically uses EMS memory, XMS memory, or disk storage, whichever is available.

Option switch precedence

Switches are read and processed in lexical order. Any subsequent switch overrides any previous switch setting.
  1. The optlinks.cfg configuration file.
  2. The LINK environment variable.
  3. The OPTLINKS environment variable.
  4. Command line and indirect response file in the order supplied. Recognizes the most recent switch syntax selected.

Configuration

To specify default operating parameters not available through the IDDE, modify the file named optlinks.cfg. You can supply option switches that you may regularly use or desire as default. When called, optlinks.cfg, an ASCII file, is read by OPTLINK from its home directory, unless optlinks.cfg already exists in the current directory. OPTLINK recognizes the "#" as a comment character and thus ignores the remainder of the line.

Environment Variables

The environment variable names recognized are LIB, TMP, LINK, OPTLINKS and OBJ. OPTLINK also searches the PATH environment variable to locate certain executable files. The environment variables are typically defined in autoexec.bat or another batch file.
LIB
Supplies paths to search for libraries, object files, and resource files. See also /SCANLIB.

TMP
Supplies a path used by OPTLINK for its virtual memory file. Otherwise, the current directory is used. OPTLINK's performance can be severely slowed when its virtual memory file must be stored across a network.

LINK
Supplies linker switch settings. Recognized for Microsoft LINK compatibility. See also /SCANLINK.

OPTLINKS
Supplies linker switch settings. OPTLINKS is scanned after the LINK variable and can override any previous settings specified.

OBJ
Supplies paths to be searched by OPTLINK for .obj files not found in the current working directory. Paths specified by OBJ are always searched before any paths specified by the LIB environment variable.

PATH
Supplies paths to be searched by OPTLINK for any stub .exe file specified in the .def file.
For example, to select the linker option /IGNORECASE as the default, the following command could be used:
set OPTLINKS=/IG 
The environment variables can be overridden by command line input. Every switch can be changed, since it has an opposite.

Versions of OPTLINK

  • linknd.dll DLL version for use in IDDE.
  • link.exe Win32 command line version.
  • lnkr.exe DOS real mode version (obsolete).

Optimizing Linker Performance

For fast execution of the linker, follow these guidelines.
  • Disable relocation checks by using the /NORELOCATIONCHECK option.

  • If the /MAP option is being used to create a .map file, disable sorting of the map by using the /MAP:address option.

  • If the /MAP option is being used to create a .map file, disable placement of line number information in the map by using the /NOLINENUMBERS option (the default).

  • If the executable file is a Windows program, use the /RC option to add resources to the executable file.

  • If running OPTLINK from a Windows DOS box, use the DPMI version (link.exe) and set the DOS box .pif file parameters in dosprmpt.pif as follows:

    XMS required: 0XMS memory not locked (-1 desired)
    EMS required: 0EMS memory not locked (-1 desired)

  • Configure a small RAM disk so more EMS, XMS, or DPMI memory is available for OPTLINK.

  • Avoid linking to a network server.


Operational Modes

OPTLINK can get commands from the command line, interactively, or from an indirect file. All input on the command line should be in a series of parameters. For the interactive mode at a console, use link.exe. Using indirect file operation, you can enter prompt-like responses into a file. The specific file name is passed to OPTLINK on the command line and is read by OPTLINK for its linker input.

In all modes of operation, OPTLINK commands, switches and parameters are the same.

Command Line and Interactive Operation

Command line operation of OPTLINK uses the following syntax:
LINK obj[,out[,map[,lib[,def[,res]]]]] 
For interactive operation input LINK followed by any option switches (described later) and press Enter. OPTLINK then prompts for responses. Option switches may be entered after any response.

OPTLINK parameters include the following:

obj
One or more input (.obj) file names. If multiple names are supplied, they may be joined by "+" characters or blank spaces, but the comma separates the four parts of the command line. If more names are required than are able to fit onto the command line, a "+" immediately before the end of the line makes the list continue to the next line.

Wild-card characters "?" and "*" to specify multiple files are allowed. If specific names are preceded or followed by wild-card references, those modules specifically named will not be included twice. At the end of the input list, use specific data or a "+" character to invoke the default choices. If neither is supplied, operation switches to the interactive mode, and OPTLINK prompts for the remaining inputs. The "#" character tells OPTLINK to ignore the rest of the comment characters following on that line.

out
Specifies the name for the output executable or dynamic link library. If no extension is supplied, an .exe file is generated; if the extension is .com, a .com file is created automatically, and if extension is .sys, a binary device driver format file is created. To skip this entry and cause OPTLINK to default to the executable type provided in a definition file or option switch, supply a comma or press the Enter key in the interactive mode. If no directive or option switch is supplied, the default file name (that of the first .obj file) with .exe extension will be created.

map
The file name for map output. Using an Enter key while in the interactive mode suppresses all map output, unless previously configured with a /MAP suggested file name. See the related switch descriptions (/MAP, /XREF, /LINENUMBERS, or /DETAILEDMAP).

lib
Searches in order one or more library names to resolve any symbols not defined in the input .obj files. Wild card references can be used as with the obj entry. The libraries are searched in the order specified here. Use the "+" character to separate multiple libraries. Use the "+" continuation character that comes before the end-of-line or before the comment (#) string to extend past the end of the line.

The lib entry may be either a single file name or a pathname (with trailing "\") to a directory containing the libraries.

OPTLINK searches library files in the order that they are presented to it: that is, those named on the command line, and then those referenced by embedded commands in object modules. To locate each library file for which no path is specified, OPTLINK first searches the current directory followed by any paths specified in the LIB variable, and finally any paths named in the command line.

def
The module definition file name. Optionally supplied, this file contains program linking information.

res
One or more resource (.RES) file names. When supplied, OPTLINK performs the bind step of the Resource Compiler while linking. This eliminates the need to use a Resource Compiler to combine resources to the output file.
Also like the .OBJ entry, wild-card references may be used. The libraries will be searched in the order specified. The same separators used for the obj portion apply, and the res entry may also be extended past the end of a line by means of the continuance character (+) immediately before the end of line or comment (#) string.

Examples of command line operation

The simplest method of using OPTLINK is to provide only the name of the .obj file to be used and accept the default suggestions for all other parameters:
C> LINK DUMP;
OPTLINK Copyright (C) Digital Mars 1989-2001.
All rights reserved.
OPTLINK's sign-on copyright message is displayed on the console as shown above.

If the .obj file does not exist in the current directory or in any of the paths supplied by the environment variables, OPTLINK sends an error message:

C> LINK DUMPER;
OPTLINK Copyright (C) Digital Mars 1989-2001 
All rights reserved.
DUMPER.OBJ 
Error 2: File Not Found DUMPER.OBJ 
If there is not a semicolon on a short command line, OPTLINK reverts to the interactive mode after all command line input is used, as the following examples indicate:
C> LINK DUMP,,MYMAP;
OPTLINK Copyright (C) Digital Mars 1989-2001 
All rights reserved. 

C> LINK DUMP,,MYMAP
OPTLINK Copyright (C) Digital Mars 1989-2001 
All rights reserved.
Libraries and Paths: (.lib): 
Definition File: (.def):
Resource Files: (.res) 

Examples of interactive operation

Use the Enter key at the Map File: prompt.
C> LINK
OPTLINK Copyright (C) Digital Mars 1989-2001 
All rights reserved.
OBJ Files: (.obj): curset 
Output File: (curset.exe):
Map File: (.map): 
Libraries and Paths: (.lib):
Definition File: (.def): 
Resource Files: (.res) 
Using the "+" and the "#" comment characters causes multiple responses at the OBJ Files: prompt:
C> LINK
OPTLINK Copyright (C) Digital Mars 1989-2001 
All rights reserved.
OBJ Files: (.obj): file1+	# this is the first object file
OBJ Files: (.obj): file2+ 
OBJ Files: (.obj): file3	# this is the last
Output File: (file1.exe): myfile 
Map File: (.map):
Libraries and Paths: (.lib): mylib 
Definition File: (.def):
Resource Files: (.res) 
Or use multiple file names on the same line:
C> LINK
OPTLINK Copyright (C) Digital Mars 1989-2001 
All rights reserved. OBJ Files: (.obj): file1 file2 file3 
Output File: (file1.exe): myfile
Map File: (.map): 
Libraries and Paths: (.lib): mylib
Definition File: (.def): 
Resource Files: (.res) 

Indirect File Operation

For more file names or option switches than the command line can hold, use indirect file operation to effectively eliminate any limit on the amount of information supplied to OPTLINK. For most complicated applications, this is the best choice.

OPTLINK uses the following syntax for indirect file operation:

LINK @indirfile
where indirfile is the name of an indirect response file. During interactive operation at each prompt, the indirect response file has a number of text lines.

Example of indirect file operation

If the file lnklst contains the following lines:
DUMP
NEWEXE 
MYMAP; 
then the following command lines are equivalent except that line length in an indirect file is unlimited, while DOS limits the command line to a maximum of 125 characters.
LINK @LNKLST 
LINK DUMP,NEWEXE,MYMAP; 
It is legal to use combinations of indirect file operation and either command line or interactive operation. OPTLINK accepts more than one indirect file specification. There are only two constraints: the indirect files that are combined must result in a valid sequence of responses and cannot be nested more than 10 deep.

Note: It is possible for a valid file name to actually begin with the "@" character. To keep such a file from being erroneously recognized by OPTLINK as an indirect response file, enclose the entire name of the file in quotation marks.

Command formats

All three methods of providing command input use exactly the same format for input information.

File name usage

OPTLINK uses the same file-naming conventions as those supplied by the host operating system. If a file extension is supplied, it must be separated from the name by a period. If none is supplied, OPTLINK assumes or creates a default value as described below.

The file name may be preceded by a pathname, which may be either absolute or relative and may include a drive specifier. If no path is specified, OPTLINK searches only the current working directory for the file or creates it in the current working directory. If a drive is specified, but no path, only the current directory for the specified drive is used.

Default file extensions

If the file extension is omitted from a file name, OPTLINK assumes or creates a default extension for the file that is determined by the command with which the file name is associated. For instance, the default extensions for the file names supplied with the OBJ Files: prompt is .obj and for the Map File: prompt is .map. The defaults follow and are described later for each command that assumes or creates one. To refer to a file that has no extension and without using the default, add the "." separator after the file name to tell OPTLINK not to supply an extension.
.def
Module Definition File
.exe
Executable files created by OPTLINK
.lib
Library files
.map
Listing of addresses generated by OPTLINK
.obj
Object files (in OMF format)
.cfg
Configuration file for default OPTLINK settings
.res
Resource files
none
Indirect command file used by OPTLINK

Special file names

Since OPTLINK uses standard I/O for keyboard input and screen output, device names can be used in place of file names and you may redirect input and output. The device names are:
AUX
refers to an auxiliary device (usually the same as COM1)
CON
refers to the console (keyboard input or display output)
PRN
refers to the printer (usually the same as LPT1) NUL a null (nonexistent) file; if given as an input name, returns end-of-file; if given as an output name, no output is generated
To redirect OPTLINK input and output from and to files, use the standard redirection keys. For example, the following command redirects OPTLINK output (including all prompts) to file link.log instead of to the screen:
LINK UTILITY; >LINK.LOG