C Libraries – Quick Guide
Everyone knows core of the C language is small and very simple to use, and special functionality is provided in the form of libraries of ready-made functions. Some of C libraries are provided for you, giving you access to many special abilities without needing to reinvent the wheel. You can also make your own, but to do so you need to know how your operating system builds libraries.
C Libraries are files of ready-compiled code which we can merge with a C program at compilation time. Each library comes with a number of associated header files which make the functions easier to use.
For example, there are libraries of mathematical functions, string handling functions and input/output functions and graphics libraries. It is up to every programmer to make sure that libraries are added at compilation time by typing an optional string to the compiler.
cc -o program_name prog.c –lm
when you compile the program. The ‘-lm’ means: add in ‘libm’. If we wanted to add in the socket library ‘libsocket.a’ to do some network programming as well, we would type
cc -o program_name prog.c -lm –lsocket
These C libraries are not included automatically because it would be a waste for the compiler to add on lots of code for maths functions, say, if they weren’t needed. When library functions are used in programs, the appropriate library code is included by the compiler, making the resulting object code often much longer.
C Libraries with Examples
C Libraries are supplemented by header files which define macros, data types and external data to be used in conjunction with the libraries. Once a header file has been included, it has effectively added to the list of reserved words and commands in the language. You cannot then use the names of functions or macros which have already been defined in libraries or header files to mean anything other than what the library specifies.
The most commonly used header file is the standard input/output library which is called ‘stdio.h’. This belongs to a subset of the standard C library which deals with file handling. The ‘math.h’ header file belongs to the math- ematics library ‘libm.a’. Header files for libraries are included by adding to the source code:
#include header.h
at the top of a program file. For instance:
#include "myheader.h"
includes a personal header file which is in the current directory. Or
#include <stdio.h>
includes a file which lies in a standard directory like ‘/usr/include’. The #include directive is actually a command to the C preprocessor, Some functions can be used without having to include library files or special libraries explicitly since every program is always merged with the standard C library, which is called ‘libc’
#include <stdio.h> main () { printf ("C standard I/O file is included\n"); printf ("Hello world!");
The program wishing to use a mathematical function such as cos would need to include a mathematics library header file.
#include <stdio.h> #include <math.h> main () { double x,y; y = sin (x); printf ("Maths library ready"); }
The particular operating system might require its own special library for certain operations such as using a mouse or for opening windows in a GUI environment, for example. These details will be found in the local manual for a particular C compiler or operating system.