Augmenting the Default Linker Search Path with -L

The -Loption to ld also allows you to add additional directories to the search path. If -Llibpath is specified, ld searches the libpath directory before the default places.

For example, suppose you have a locally developed version of libc, which resides in the directory /usr/local/lib. To make ld find this version of libc before the default libc, use the -Loption as follows:

$ ld prog.o -L /usr/local/lib -lc

If LPATH is set, then the -Loption specifies the directories to search before the directories specified in LPATH.

Augmenting the Default Linker Search Path with +origin

The +origin option to ld instructs the linker to search for the library in the directory from which the object module originated.

The +origin option only applies to the shared library specified directly afterwards, for example, libc.so or -lc.

At runtime, if the dynamic loader cannot find the library listed in the path specified by $ORIGIN, it attempts to search paths according to the search path algorithm described above.

The syntax is as follows:

$ ld main.o +origin -lc

or

$ ld main.o +origin /usr/lib/hpux32/libc.so

Using $ORIGIN

You can use the $ORIGIN string in LD_LIBRARY_PATH, SHLIB_PATH, RUNPATH (the embedded path or RPATH), or in the path of a shared library in the shared library list. If the DF_ORIGIN flag is set, the loader determines the path of the current load module when the load module is first loaded. If the DF_ORIGIN flag is not set, the loader determines the path of the current load module when the loader first encounters $ORIGIN, whether it is in LD_LIBRARY_PATH, SHLIB_PATH, RUNPATH, or the shared library name in the shared library list.

To add $ORIGIN to the environment variables LD_LIBRARY_PATH or SHLIB_PATH, place

$ORIGIN in the value of these environment variables. To add $ORIGIN to the RUNPATH, use the linker options +b or -L. To add $ORIGIN to the path of a shared library in the shared library list, use the linker option +origin.

+origin -lx

or

+origin shared_library_name

(You can use only the +origin option before the -loption or the name of a shared library.) The option causes the linker to add $ORIGIN before the shared library name in the shared library list and set the DF_ORIGIN flag for the output module. At runtime, the dynamic loader determines the directory of the parent module (object module, shared library, or executable) and replaces $ORIGIN for that directory name. For example,

$ ld main.o +origin libx.so -L -lc

Using Linker Commands 31

Page 31
Image 31
HP UX Software Transition Kit (STK) manual Using $ORIGIN, Augmenting the Default Linker Search Path with -L