[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
General Programming Concepts: Writing and Debugging Programs

Chapter 24. Subroutines, Example Programs, and Libraries

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.

Notes:
  1. All programs that handle multibyte characters, wide characters, or locale-specific information must call the setlocale subroutine at the beginning of the program. See "National Language Support Subroutines Overview" for more information.
  2. Programming in a multi-threaded environment requires reentrant subroutines to ensure data integrity. See the "List of Multi-threaded Programming Subroutines" .

Related Information

setlocale subroutine

List of 128-bit long double Numerical Manipulation Subroutines

List of Character Manipulation Subroutines

List of Converter Subroutines

List of Executable Program Creation Subroutines

List of Files and Directories Subroutines

List of FORTRAN BLAS Level 1: Vector-Vector Subroutines

List of FORTRAN BLAS Level 2: Matrix-Vector Subroutines

List of FORTRAN BLAS Level 3: Matrix-Matrix Subroutines

List of Layout Library Subroutines

List of Logical Volume Subroutines

List of Memory Manipulation Services

List of Memory Mapping Services

List of Message Facility Subroutines

List of Numerical Manipulation Subroutines

List of long long Integer Numerical Manipulation Subroutines

List of 128-Bit long double Numerical Manipulation Subroutines

List of Processes Subroutines

List of Multi-threaded Programming Subroutines

List of Programmer's Workbench Library Subroutines

List of Security and Auditing Subroutines

List of String Manipulation Subroutines

List of Threads Subroutines

List of Input Method Subroutines

List of National Language Support Subroutines

Programming Example for Manipulating Characters

Searching and Sorting Example Program

List of Operating System Libraries

librs2.a Library

List of Time Data Manipulation Services in AIX Version 4.3 System Management Guide: Operating System and Devices.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]