Subroutines, example programs, and libraries

This topic provides information about what subroutines are, how to use them, and where they are stored.

Subroutines are stored in libraries to conserve storage space and to make the program linkage process more efficient. A library is a data file that contains copies of a number of individual files and control information that allows them to be accessed individually. The libraries are located in the /usr/ccs/lib and /usr/lib directories. By convention, most of them have names of the form libname.a where name identifies the specific library.

All include statements should be near the beginning of the first file being compiled, usually in the declarations section before main( ), and must occur before using any library functions. For example, use the following statement to include the stdio.h file:

#include <stdio.h>

You do not need to do anything special to use subroutines from the Standard C library (libc.a). The cc command automatically searches this library for subroutines that a program needs. However, if you use subroutines from another library, you must tell the compiler to search that library. If your program uses subroutines from the library libname.a, compile your program with the flag -lname (lowercase L). The following example compiles the program myprog.c, which uses subroutines from the libdbm.a library:

cc myprog.c -ldbm

You can specify more than one -l (lowercase L) flag. Each flag is processed in the order specified.

If you are using a subroutine that is stored in the Berkeley Compatibility Library, bind to the libbsd.a library before binding to the libc.a library, as shown in the following example:

cc myprog.c -lbsd

When an error occurs, many subroutines return a value of -1 and set an external variable named errno to identify the error. The sys/errno.h file declares the errno variable and defines a constant for each of the possible error conditions.

In this documentation, all system calls are described as subroutines and are resolved from the libc.a library. The programming interface to system calls is identical to that of subroutines. As far as a C Language program is concerned, a system call is merely a subroutine call. The real difference between a system call and a subroutine is the type of operation it performs. When a program invokes a system call, a protection domain switch takes place so that the called routine has access to the operating system kernel's privileged information. The routine then operates in kernel mode to perform a task on behalf of the program. In this way, access to the privileged system information is restricted to a predefined set of routines whose actions can be controlled.

Note:
  1. The following list represents the wString routines that are obsolete for the 64 bit libc.a. Their corresponding 64 bit libc.a equivalents are included. The routines for the 32 bit libc.a can be found in the wstring subroutine.
    32 Bit only                 64 Bit Equivalent
    
    wstrcat                     wcscat
    wstrchr                     wcschr
    wstrcmp                     wcscoll
    wstrcpy                     wcscpy
    wstrcspn                    wcscspn
    wstrdup                     Not available and has no
                                equivalents in the 64 bit libc.a
    wstrlen                     wcslen
    wstrncat                    wcsncat
    wstrncpy                    wcsncpy
    wstrpbrk                    wcspbrk
    wstrrchr                    wcsrchr
    wstrspn                     wcsspn
    wstrtok                     wcstok
  2. All programs that handle multibyte characters, wide characters, or locale-specific information must call the setlocale subroutine at the beginning of the program.
  3. Programming in a multithreaded environment requires reentrant subroutines to ensure data integrity.