| United States-English |
|
|
|
![]() |
HP-UX Linker and Libraries User's Guide: HP 9000 Computers > Chapter 3 Linker TasksUsing the Compiler to Link |
|
In many cases, you use your compiler command to compile and link programs. Your compiler uses options that directly affect the linker. 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. 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:
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 ”. 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.
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. The command for telling the linker to use an archive version of libm from the C command line is:
The command for telling the linker to use an archive version of libm is:
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:
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:
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”. 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:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||