Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Linker and Libraries User's Guide: HP 9000 Computers > Chapter 3 Linker Tasks

Using the Compiler to Link

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

In many cases, you use your compiler command to compile and link programs. Your compiler uses options that directly affect the linker.

Changing the Default Library Search Path with -Wl, -L

By default, the linker searches the directory /usr/lib and /usr/ccs/lib for libraries specified with the -l compiler option. (If the -p or -G compiler option is specified, then the linker also searches the profiling library directory /usr/lib/libp.)

The -L libpath option to ld augments the default search path; that is, it causes ld to search the specified libpath before the default places. The C compiler (cc), the C++ compiler (CC), the POSIX FORTRAN compiler (fort77), and the HP Fortran 90 compiler (f90) recognize the -L option and pass it directly to ld. However, the HP FORTRAN compiler (f77) and Pascal compiler (pc) do not recognize -L; it must be passed to ld with the -Wl option.

Example Using -Wl, -L

For example, to make the f77 compiler search /usr/local/lib to find a locally developed library named liblocal, use this command line:

$f77 prog.f -Wl,-L,/usr/local/lib -llocal

(The f77 compiler searches /opt/fortran/lib and /usr/lib as default directories.)

To make the f90 compiler search /usr/local/lib to find a locally developed library named liblocal,, use this command line:

$f90 prog.f90 -L/usr/local/lib -llocal

(The f90 compiler searches /opt/fortran90/lib and /usr/lib as default directories.)

For the C compiler, use this command line:

$ cc -Aa prog.c -L /usr/local/lib -llocal 

The LPATH environment variable provides another way to override the default search path. For details, see “Changing the Default Library Search Path with -L and LPATH ”.

Getting Verbose Output with -v

The -v option makes a compiler display verbose information. This is useful for seeing how the compiler calls ld. For example, using the -v option with the Pascal compiler shows that it automatically links with libcl, libm, and libc.

$ pc -v prog.p
/opt/pascal/lbin/pascomp prog.p prog.o -O0
LPATH = /usr/lib/pa1.1:/usr/lib:/opt/langtools/lib
/usr/ccs/bin/ld /opt/langtools/lib/crt0.o -z prog.o -lcl -lm -lc
unlink prog.o

Passing Linker Options from the Compiler Command with -Wl

The -Wl option passes options and arguments to ld directly, without the compiler interpreting the options. Its syntax is:

-Wl,arg1 [,arg2]...

where each argn is an option or argument passed to the linker. For example, to make ld use the archive version of a library instead of the shared, you must specify -a archive on the ld command line before the library.

Example Using -Wl

The command for telling the linker to use an archive version of libm from the C command line is:

$ cc -Aa mathprog.c -Wl,-a,archive,-lm,-a,default 

The command for telling the linker to use an archive version of libm is:

$ ld /opt/langtools/lib/crt0.o mathprog.o -a archive -lm \
-a default -lc

Renaming the Output File with -o

The -o name option causes ld to name the output file name instead of a.out. For example, to compile a C program prog.c and name the resulting file sum_num:

$ cc -Aa -o sum_num prog.c           Compile using -o option.  
$ sum_num Run the program.
Enter a number to sum: 5
The sum of 1 to 5: 15

Specifying Libraries with -l

Sometimes programs call routines not contained in the default libraries. In such cases you must explicitly specify the necessary libraries on the compile line with the -l option. The compilers pass -l options directly to the linker before the default libraries.

For example, if a C program calls library routines in the curses library (libcurses), you must specify -lcurses on the cc command line:

$ cc -Aa -v cursesprog.c -lcurses
...
/usr/ccs/bin/ld /opt/langtools/lib/crt0.o -u main \
cursesprog.o -lcurses -lc
cc: Entering Link editor.

Linking with the crt0.o Startup File in 32-bit mode

Notice also, in the above example, that the compiler linked cursesprog.o with the file /opt/langtools/lib/crt0.o. This file contains object code that performs tasks which must be executed when a program starts running — for example, retrieving any arguments specified on the command line when the program is invoked. For details on this file, see crt0(3) and “The crt0.o Startup File”.

Suppressing the Link-Edit Phase with -c

The -c compiler option suppresses the link-edit phase. That is, the compiler generates only the .o files and not the a.out file. This is useful when compiling source files that contain only subprograms and data. These may be linked later with other object files, or placed in an archive or shared library. The resulting object files can then be specified on the compiler command line, just like source files. For example:

$ f77 -c func.f             Produce .o for func.f.         
$ ls func.o
func.o Verify that func.o was created.
$ f77 main.f func.o Compile main.f with func.o $ a.out Run it to verify it worked.
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1997 Hewlett-Packard Development Company, L.P.