Linker and Libraries Guide

Appendix A Link-Editor Quick Reference

The following sections provide a simple overview, or cheat sheet, of the most commonly used link-editor scenarios. See "Link-Editing" for an introduction to the kinds of output modules generated by the link-editor.

The examples provided show the link-editor options as supplied to a compiler driver, this being the most common mechanism of invoking the link-editor. In these examples we use cc(1). See "Using a Compiler Driver".

The link-editor places no meaning on the name of any input file. Each file is opened and inspected to determine the type of processing it requires. See "Input File Processing".

Shared objects that follow a naming convention of libx.so, and archive libraries that follow a naming convention of libx.a, can be input using the -l option. See "Library Naming Conventions". This provides additional flexibility in allowing search paths to be specified using the -L option. See "Directories Searched by the Link-Editor".

The link-editor basically operates in one of two modes, static or dynamic.

Static Mode

Static mode is selected when the -d n option is used, and enables you to create relocatable objects and static executables. Under this mode, only relocatable objects and archive libraries are acceptable forms of input. Use of the -l option will result in a search for archive libraries.

Creating a Relocatable Object


$ cc -dn -r -o temp.o file1.o file2.o file3.o .....

Creating a Static Executable

The use of static executables is limited. Static executables usually contain platform-specific implementation details that restricts the ability of the executable to be run on an alternative platform. Many implementations of Solaris libraries depend on dynamic linking capabilities, such as dlopen(3DL) and dlsym(3DL). See "Loading Additional Objects". These capabilities are not available to static executables.


$ cc -dn -o prog file1.o file2.o file3.o .....

The -a option is available to indicate the creation of a static executable. The use of -d n without a -r implies -a.

Dynamic Mode

Dynamic mode is the default mode of operation for the link-editor. It can be enforced by specifying the -d y option, but is implied when not using the -d n option.

Under this mode, relocatable objects, shared objects and archive libraries are acceptable forms of input. Use of the -l option will result in a directory search, where each directory is searched for a shared object. If no shared object is found, the same directory is then searched for an archive library. A search only for archive libraries can be enforced by using the -B static option. See "Linking With a Mix of Shared Objects and Archives".

Creating a Shared Object

The following example combines the above points:


$ cc -c -o foo.o -Kpic -xregs=no%appl foo.c
$ cc -M mapfile -G -o libfoo.so.1 -z text -z defs -B direct -z lazyload \
-z combreloc -z ignore -R /home/lib foo.o -L. -lbar -lc

The following example combines the above points:


$ cc -M mapfile -G -o libfoo.so.1 -z text -z defs -B direct -z lazyload \
-z combreloc -z ignore -R /home/lib -h libfoo.so.1 foo.o -L. -lbar -lc
$ ln -s libfoo.so.1 libfoo.so

Creating a Dynamic Executable

The following example combines the above points:


$ cc -o prog -R /home/lib -z ignore -z lazyload -B direct -L. \
-lfoo file1.o file2.o file3.o .....