The libraries Field

You can specify one or more standard or import libraries (not DLLs) in the libraries field. If you name more than one library, separate the names with a plus sign (+) or a space. To extend libraries to the following line, type a plus sign (+) as the last character on the current line, press ENTER, and continue. Do not split a name across lines. If you specify the base name of a library without an extension, LINK assumes a default .LIB extension.

If no library is specified, LINK searches only the default libraries named in the object files to resolve unresolved references. If one or more libraries are specified, LINK searches them in the order named before searching the default libraries.

You can tell LINK to search additional directories for specified or default libraries by giving a drive name or path specification in the libraries field; end the specification with a backslash (\). (If you don't include the backslash, LINK assumes that the specification is for a library file instead of a directory.) LINK looks for files ending in .LIB in these directories.

You can specify a total of 32 paths or libraries in the field. If you give more than 32 paths or libraries, LINK ignores the additional specifications without warning you.

You might need to specify library names to:

Use a default library that has been renamed.

Specify a library other than the default named in the object file (for example, a library that handles floating-point arithmetic differently from the default library).

Search additional libraries.

Find a library that is not in the current directory and not in a directory specified by the LIB environment variable.

Default Libraries

Most compilers insert the names of the required language libraries in the object files. LINK searches for these default libraries automatically; you do not need to specify them in the libraries field. The libraries must already exist with the name specified in the object file. Default-library names usually refer to combined libraries built and named during setup; consult your compiler documentation for more information about default libraries.

To make LINK ignore the default libraries, use the /NOD option. This leaves unresolved references in the object files. Therefore, you must use the libraries field to specify the alternative libraries that LINK is to search.

Import Libraries

You can specify import libraries created by the IMPLIB utility anywhere you can specify standard libraries. You can also use the LIB utility to combine import libraries and standard libraries. These combined libraries can then be specified in the libraries field. For more information on LIB, see Chapter 19. For information on IMPLIB, see topic .

How LINK Resolves References

LINK searches static libraries to resolve external references. A static library is either a standard library created by the LIB utility or an import library created by the IMPLIB utility.

LINK searches object files and libraries for a definition of each external reference. When LINK finds a needed definition in a module in a library, LINK adds the entire module (but not necessarily all modules in the library) to the program.

You provide a library to LINK in the following ways:

Specify the name of a library in the libraries field.

Specify the name of a library as a load library in the objects field. A load library adds all its modules to the program. For more information, see “Load Libraries”.

Compile a program that uses definitions provided in a default library for that compiler. The compiler places a library comment record in the object file. LINK uses the library named in this record.

Embed a library comment record in the object file by using the comment pragma in a C program. This record precedes a record for a default library placed in the object file by the compiler; therefore, LINK looks in this library before it searches a default library named in the same object file.

LINK first looks for a definition in files specified in the objects field, then it looks in libraries specified in the libraries field. The search order is the order in which the files are specified in the fields. LINK then looks in libraries specified in comment records in the object files, again in the specified order.

If LINK cannot find a needed definition, it issues an error message:

unresolved external

If a reference is defined in more than one library, LINK uses the first definition it finds as it searches the libraries in order. A duplicate definition may not be a problem if the later definition is in a module that is not linked into the program. However, if the duplicate definition is in a module that contains another needed definition, that module is linked into the program, and the duplicate definition causes an error:

symbol defined more than once

Multiple definitions can also cause a problem if LINK is using extended dictionaries in libraries. An extended dictionary is a summary of the definitions contained in all modules of a library. LINK uses this summary to speed the process of searching libraries. If LINK finds a previously resolved reference listed in an extended dictionary, it assumes that a duplicate definition exists and issues an error message:

symbol multiply defined, use /NOE

If this error occurs, link your program using the /NOE option.

How LINK Searches for Library Files

When searching for a library, LINK looks in the following locations in this order:

1.The directory specified for the file, if a path is included. (The default libraries named in object files by Microsoft compilers do not include path specifications.)

2.The current directory.

3.Any directories specified in the libraries field.

4.Any directories specified in the LIB environment variable.

If LINK cannot locate a library file, it prompts you to enter the location. The /BATCH option disables this prompting.

Example

The following is a specification in the libraries field:

C:\TESTLIB\ NEWLIBV3 C:\MYLIBS\SPECIAL

LINK searches NEWLIBV3.LIB first for unresolved references. Since no directory is specified for NEWLIBV3.LIB, LINK looks in the following locations in this order:

1.The current directory

2.The C:\TESTLIB\ directory

3.The directories in the LIB environment variable

If LINK still cannot find NEWLIBV3.LIB, it prompts you with the message:

Enter new file spec:

Enter either a path to the library or a path and filename for another library.

If unresolved references remain after LINK searches NEWLIBV3.LIB, it then searches the library C:\MYLIBS\SPECIAL.LIB. If LINK cannot find this library, it prompts you as described previously for NEWLIBV3.LIB. If there are still unresolved references, LINK searches the default libraries.