Making a Library
Since the Ayn Rand and bubble_sort social occasion in the previous program are useful , you will belike want to reuse them in other programs you write . you’re able to put them into a service program subroutine library to make their reuse gentle .
Every library consist of two parts : a coping file cabinet and the genuine computer code file . The lintel file , normally denoted by a.hsuffix , contains information about the program library that programs using it need to know . In cosmopolitan , the header file contains constants and types , along with prototypes for mathematical function usable in the library . get into the follow coping file and redeem it to a file namedutil.h .
These two line of merchandise are role prototypes . The word " extern " in C defend functions that will be linked in later . If you are using an old - style compiling program , take the parameters from the argument list ofbubble_sort .
Enter the following code into a single file namedutil.c .
observe that the file cabinet let in its own header file cabinet ( util.h ) and that it use quotes instead of the symbols < and > , which are used only for system libraries . As you may see , this looks like normal hundred computer code . Note that the variablerand_seed , because it is not in the heading Indian file , can not be seen or modified by a program using this library . This is called entropy concealing . bring the wordstaticin front ofintenforces the concealment completely .
embark the postdate main broadcast in a data file namedmain.c .
This computer code includes the utility program library . The main benefit of using a library is that the code in the principal programme is much shorter .
Compiling and Running with a Library
To compile the library , typecast the following at the mastery argument ( take you are using UNIX ) ( replace gcc with cubic centimetre if your system expend cc ):
The - ccauses the compiling program to produce an object file for the library . The object file contains the library ’s machine codification . It can not be executed until it is linked to a program file that contain a main function . The simple machine code resides in a separate file namedutil.o .
To compile the main course of study , typecast the following :
This line make a file namedmain.othat contains the machine codification for the main program . To make the terminal executable that carry the machine code for the entire programme , link the two target file by typing the following :
This linksmain.oandutil.oto form an executable namedmain . To run it , typemain .
Makefiles make work out with program library a bit easier . You ’ll find out about makefiles on the next page .