ld Manual

Introduction to ld
ld Manual529650.001
1-14
Linking Archive Files
You can specify archive files on the ld command line directly and with the options -l or
-lib. Specifying archive files with options is usually easier to understand than specifying
archive files on the command line.
ld resolves references using archive files in the order they are specified, either on the
command line directly or with -l or -lib options.
ld searches an archive file to resolve references. When ld finds object files that
satisfy references, it copies them from the archive file to the target object file. Unlike
Binder, ld copies the entire object file, not only the individual procedure.
For example, given object files obja, objb, and objc and archive files arc1 and
arc2 and this ld command line:
ld obja objb -lib arc1 objc arc2 -o myexec
ld performs these steps:
1. Copies obja and objb.
2. Searches arc1 for unresolved references in obja and objb. For those references
satisfied in arc1, ld copies the object file satisfying the reference.
3. Copies objc.
4. Searches arc2 for unresolved references in obja, objb, and objc. ld also
searches arc2 for unresolved references in object files copied from arc1.
A duplicate reference in an object file or an archive file is an error.
Unlike libraries, which must be specified only once in a linking operation, you might
need to specify archives more than once in the same linking operation. For example, if
the object program obj calls functions fun1 and fun3, and if archive ar1 contains
functions fun1 and fun2 in separate archives or object files, and archive ar2 contains
function fun3, which calls fun2, the command:
ld obj.o -l ar1.a -l ar2.a
links obj, ar1 and ar2. ld resolves references to fun1 and fun3, but fun2 is
unresolved.
This command:
ld obj.o -l ar1.a -l ar2.a -l ar1.a
links obj, ar1 and ar2 completely and ld resolves all references.