Linking Archived Libraries with -noshared

Use the -nosharedoption if you need to link with all archive libraries. The linker outputs an archive-bound executable.

NOTE: You cannot link in shared libraries if you specify this option.

In the following example, the linker only looks for:

/usr/lib/hpux32/libfoo.a and /user/lib/hpux32/libc.a:

$ ld /usr/ccs/hpux32/crt0.o main.o -noshared -L. -lfoo -lc

If you specify a shared library libbar.so with this option, the linker displays the error message:

ld: The shared library "libbar.so" cannot be processed in a static link. Fatal error.

Exporting Symbols with +e

The +e option allows you to hide and export symbols. Exporting a symbol makes the symbol a global definition, which can be accessed by other object modules or libraries. The +e option exports the symbol and hides from export all other global symbols not specified with +e. In essence, -hand +e provide two different ways to do the same thing.

The syntax of the +e option is:

+e symbol

Example Using +e

Suppose you want to build a shared library from an object file that contains the following symbol definitions as displayed by the nm command:

$ nm -p sem.o

0000000000 U $global$

1073741824 d $THIS_DATA$

1073741864 b $THIS_BSS$

0000000004 cS sem_val

0000000000 T check_sem_val

0000000036 T foo

0000000000 U printf

0000000088 T bar

0000000140 T sem

In this example, check_sem_val, foo, bar, and sem are all global definitions. To create a shared library where check_sem_val is a hidden, local definition, you could use either of the following commands:

$ ld

-b

-h

check_sem_val

sem.o -o libsem.so

//One -h option.

$ ld

-b

+e

foo +e bar +e

sem sem.o -o libsem.so

//Three +e options.

In contrast, suppose you want to export only the check_sem_val symbol. Either of the following commands work:

$ ld

-b

-h

foo -h bar -h

sem sem.o -o libsem.so

//Three -h options.

$ ld

-b

+e

check_sem_val

sem.o -o libsem.so

//One +e option.

When to use -h versus +e

How do you decide whether to use -hor +e? In general, use -hif you simply want to hide a few symbols. And use +e if you want to export a few symbols and hide a large number of symbols. You must not run -hand +e options on the same command line. For instance, suppose you specify +e sem. This exports the symbol sem and hides all other symbols. Any additional -hoptions becomes unnecessary. If both -hand +e are used on the same symbol, the -hoverrides the +e option.

Using Linker Commands 37

Page 37
Image 37
HP UX Software Transition Kit (STK) Linking Archived Libraries with -noshared, Exporting Symbols with +e, Example Using +e