Adding Functions to the Library in C

Most of the times we either use the functions present in the standard library or we define our own functions and use them. Can we not add our functions to the standard library? And would it make any sense in doing so? We can add user-defined functions to the library. It makes sense in doing so as the functions that are to be added to the library are first compiled and then added. When we use these functions (by calling them) we save on their compilation time as they are available in the library in the compiled form

Let us now see how to add user-defined functions to the library. Different compilers provide different utilities to add/delete/modify functions in the standard library

For example, Turbo C/C++ compilers provide a utility called ‘tlib.exe’ (Turbo Librarian). Let us use this utility to add a function factorial( ) to the library

Given below are the steps to do so:

  • Write the function definition of factorial( ) in some file, say ‘fact.c’.
int factorial ( int num ) 
 int i, f = 1 ; 
 for ( i = 1 ; i <= num ; i++ ) 
 f = f * i ; 
 return ( f ) ; 
  • Compile the ‘fact.c’ file using Alt F9. A new file called ‘fact.obj’ would get created containing the compiled code in machine language.
  • Add the function to the library by issuing the command

C:>tlib math.lib + c:\fact.obj

  • Here, ‘math.lib’ is a library filename, + is a switch, which means we want to add new function to library and ‘c:\fact.obj’ is the path of the ‘.obj’ file
  • Declare the prototype of the factorial( ) function in the header file, say ‘fact.h’. This file should be included while calling the function
  • To use the function present inside the library, create a program as shown below:
#include "c:\\fact.h" 
main( ) 
 int f ; 
 f = factorial ( 5 ) ; 
 printf ( "%d", f ) ; 
  • Compile and execute the program using Ctrl F9

If we wish we can delete the existing functions present in the library using the minus ( – ) switch

Instead of modifying the existing libraries we can create our own library. Let’s see how to do this. Let us assume that we wish to create a library containing the functions factorial( ), prime( ) and fibonacci( ). As their names suggest, factorial( ) calculates and returns the factorial value of the integer passed to it, prime( ) reports whether the number passed to it is a prime number or not and fibonacci( ) prints the first n terms of the Fibonacci series, where n is the number passed to it. Here are the steps that need to be carried out to create this library. Note that these steps are specific to Turbo C/C++ compiler and would vary for other compilers

  • Define the functions factorial( ), prime( ) and fibonacci( ) in a file, say ‘myfuncs.c’. Do not define main( ) in this file.
  • Create a file ‘myfuncs.h’ and declare the prototypes of factorial( ), prime( ) and fibonacci( ) in it as shown below:

int factorial ( int ) ;
int prime ( int ) ;
void fibonacci ( int ) ;

  • From the Options menu select the menu-item ‘Application’. From the dialog that pops us select the option ‘Library’. Select OK
  • Compile the program using Alt F9. This would create the library file called ‘myfuncs.lib’

That’s it. The library now stands created. Now we have to use the functions defined in this library. Here is how it can be done.

  • Create a file, say ‘sample.c’ and type the following code in it.

include “myfuncs.h”

main( )
int f, result ;
f = factorial ( 5 ) ;
result = prime ( 13 ) ;
fibonacci ( 6 ) ;
printf ( “\n%d %d”, f, result ) ;

Note that the file ‘myfuncs.h’ should be in the same directory as the file ‘sample.c’. If not, then while including ‘myfuncs.h’ mention the appropriate path.

  • Go to the ‘Project’ menu and select ‘Open Project…’ option. On doing so a dialog would pop up. Give the name of the project, say ‘sample.prj’ and select OK.
  • From the ‘Project’ menu select ‘Add Item’. On doing so a file dialog would appear. Select the file ‘sample.c’ and then select ‘Add’. Also add the file ‘myfuncs.lib’ in the same manner. Finally select ‘Done’.
  • Compile and execute the project using Ctrl F9.

Leave a Comment