Specifying Libraries with -l and -l:

To direct the linker to search a particular library, use the -lname option. For example, to specify libc, use -lc; to specify libm, use -lm; to specify libXm, use -lXm.

Specifying Libraries (-l)

When writing programs that call routines not found in the default libraries linked at compile time, you must specify the libraries on the compiler command line with the -lxoption. For example, if you write a C program that calls POSIX math functions, you must link with libm.

The x argument corresponds to the identifying portion of the library path name - the part following lib and preceding the suffix .a or .sl. For example, for the libm.sl or libm.a library, x is the letter m:

$ cc -Aa mathprog.c -lm

The linker searches libraries in the order in which they are specified on the command line (that is, the link order). In addition, libraries specified with -lare searched before the libraries that the compiler links by default.

Using the -l: option

The -l: option works just like the -loption with one major difference: -l: allows you to specify the full base name of the library to link with. For instance, -l:libm.acauses the linker to link with the archive library /opt/langtools/lib/hpux32/libm.a, regardless of whether -ashared was specified previously on the linker command line.

The advantage of using this option is that it allows you to specify an archive or shared library explicitly without having to change the state of the -aoption. (See also “Caution When Mixing Shared and Archive Libraries” (page 118) .)

For instance, suppose you use the LDOPTS environment variable (see “Passing Linker Options with LDOPTS” (page 44) ) to set the -aoption that you want to use by default when linking. Depending on what environment you are building an application for, you may set LDOPTS to -a archive or -a shared. You can use -l: to ensure that the linker always links with a particular library regardless of the setting of the -aoption in the LDOPTS variable.

Example Using -l:

For example, even if LDOPTS were set to -a shared, the following command links with the archive libfoo.a in the directory /usr/mylibs, the archive libm.a and libc.a:

$ ld /opt/langtools/lib/hpux32/crt0.o -u main prog.o -L/usr/mylibs \ -l:libfoo.a -l:libc.a -l:libm.a

Flagging Unsatisfied Symbols with +[no]allowunsats

Use the +allowunsats option to instruct the linker to not flag unsatisfied symbols at link time. This is the default for relocatable (-r) and shared library builds (-b), and is the default behavior in PA-32 mode.

Use the +noallowunsat option to instruct the linker to flag as an error any unsatisfied symbol in the resulting output file. The linker still creates a.out, but the file does not have any execute permission bits set. This is the default for program files (same behavior as in PA-32 mode).

For example, where main() references functions foo() and bar(). The bar() resides in libbar and foo() resides in libfoo

$ ld main.o +allowunsats -L. -lbar -lc

ld: (warning) Unsatisfied symbol "foo". 1 warning.

Using Linker Commands 45

Page 45
Image 45
HP UX Software Transition Kit (STK) Specifying Libraries with -l and -l, Flagging Unsatisfied Symbols with +noallowunsats