12.9 – Creating Library files in Turbo C

by subbu on December 22, 2013

How to create library files in Turbo C

In previous session, we have discoursed how a header file can be created and used in different applications. The disadvantage with the previous method is, the total code of header file is physically adding to the source code, which results increase in the size of program (source code) and takes time to compile.

The best practice to reduce the size and compilation time is, creating a library and adding it to Turbo C library. So that, the linker adds the compiled library functions to the program’s object code that is, add after compilation of program. It saves the compilation time and reduces the size of source code.

Now we will see step by step procedure in creating a (static) library, associated header files and adding them to Turbo C library

Note: Before going further, make sure that the path is set to c:\tc\bin as explained in the previous session 1.6 How to execute a C program in command prompt using Turbo C. So that, tools required compiling (tcc) and creating library (tlib) can be accessed from any where.

Setting path to bin of Turbo C

Directory structure on the author’s machine is:
Include Directory: c:\tc\include
Lib Directory : c:\tc\lib
Output Directory: c:\tc\works
Source Directory: c:\tc

Turbo C include folder

These details may change version to version, hoping you will set according to your machine

Steps in creating and distributing library in Turbo C:

Steps in creating library

Step 1: Create the working folder (d:\fox)

All the .c files, object files, header files and library files are first created in the working folder (d:\fox) and then distributed into Turbo C include and lib folders

Navigation:
Go to the DOS shell by selecting start – run – type cmd – open
Change to d:
Create a folder fox using the DOS command md (Making directory)
Change to fox folder using the DOS command cd (Changing Directory)

d:\>md fox
d:\>cd fox
d:\fox>

Creating working folder for lib

Step 2: Create function libraries and save them with .c extension.

d:\fox>edit arith.c
/* arith.c */
int sum(int x,int y)
{
  return x+y;
}
int sub(int x,int y)
{
  return x-y;
}
int pro(int x,int y)
{
  return x*y;
}
float divide(int x,int y)
{
  return (float)x/y;
}

Select Save (Alt+S), Exit (X) to come out of the text editor

d:\fox>edit logic.c
/* logic.c */
int isEqual(int x,int y)
{
  return ((x==y)?1:0);
}
int biggest(int x,int y)
{
  return ((x>y)?x:y);
}
int smallest(int x,int y)
{
  return ((x<y)?x:y);
}

Select Save (Alt+S), Exit (X) to come out of the text editor

Step 3: Compile both the files using tcc (Turbo C Compiler) and create their object files
Use –c (small case) option to compile and create only object files

d:\fox>tcc –c arith.c arith.obj
d:\fox>tcc –c logic.c logic.obj

Compiling libraries

Step 4: Create two header files “arith.h” and “logic.h” with the signatures (prototypes) of their functions using text editor

d:\fox>edit arith.h
int sum(int x,int y);
int sub(int x,int y);
int pro(int x,int y);
float divide(int x,int y);

Select Save (Alt+S), Exit (X) to come out of the text editor

d:\fox>edit logic.h
int isEqual(int x,int y);
int biggest(int x,int y);
int smallest(int x,int y);

Select Save (Alt+S), Exit (X) to come out of the text editor

Step 5: Create a library file using tlib (Turbo C librarian) with the name “mylib.lib” with object files “arith.obj” and “logic.obj”
Use /C (capital) option to create case sensitive library
Use + to add object files

d:\fox>tlib /C mylib.lib +arith.obj+logic.obj

Step 6: Updating the knowledge of CS.LIB (c:\tc\lib\CS.LIB) of Turbo C with our library file “mylib.lib” using tlib tool.
It is because Turbo C linker links the libraries according to the entries in CS.LIB
Make sure that a copy of CS.LIB is stored some where else, so that it can be replaced if any thing goes wrong

d:\fox>tlib c:\tc\lib\CS.LIB+mylib.lib

updating cs

Step 7: Distributing the files into Turbo C folders
Copy d:\fox\mylib.lib into c:\tc\lib
Copy d:\fox\arith.h, d:\fox\logic.h into c:\tc\include

Distributing library

Using the library:

/* demo1.c */
#include<stdio.h>
#include<arith.h>
int main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
printf("Sum %d",sum(a,b));
printf("\nSubstraction %d",sub(a,b));
printf("\nProduct %d",pro(a,b));
printf("\nDivision %f",divide(a,b));
return 0;
}

Output:
Enter two numbers:
12
5
Sum 17
Subtraction 7
Product 60
Division 2.400000

Using the library

/* demo2.c */
#include<stdio.h>
#include<logic.h>
int main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
if(isEqual(a,b))
  printf("Equals");
else
  printf("Biggest %d\nSmallest %d",biggest(a,b),smallest(a,b));
return 0;
}

Output:

Enter two numbers:
12
34
Biggest 34
Smallest 12

Previous post:

Next post: