Great students like us work on the principle of Rockets.. It does not mean tht we aim to the sky..But we dn't work hard untill our tail is on fire..

Ankit - Born 2 Code - Create your own library file in C
This tutorial shows you how to create your own Library file & Header file
--------------------------------------------------------------------------------------------------------------
Creating library is very simple than writing a program.
( Here i m showing the example to add a new function swap() to C library
    --which will swap values of two integer variables )

Step 1 :
    Write your functions(i.e,Function definition without 'main' function) in a 'C' program file.
    for example:
/* swap.c */
#include<stdio.h>
void swap(int x,int y)
{
    int z;
    z=x;
    x=y;
    y=z;
    printf("nAFTER SWAPPING");
    printf("nFIRST  NUMBER >> %d",x);
    printf("nSECOND NUMBER >> %d",y);
}

Step 2 :
    Create a header file. The header file must have the function declarations of your functions(i.e,     Function prototypes).
    for example:
/* swap.h */
#ifndef __SWAP_H
    #define __SWAP_H

    #ifdef __cplusplus
    extern "C"{
    #endif

    void     _Cdecl swap(int x,int y);

    ifdef __cplusplus
    }
    endif
#endif


Step 3 :
    Compile your 'C' source code file.Compiler will produce the object code (i.e, '.obj' file).
------------Process 1--------------------
    Using TCC utility (Turbo C complier) make the obj file.
    (you will find TCC.exe in ur c:/tc/bin directory....)
    ...make sure tlib.exe and swap.c both are in same folder....
    (if u r facing any error try to run this command in command prompt)
    for example:
TCC -c swap.c swap.obj
    you will get -- swap.obj

------------Process 2--------------------
    open your source file swap.c in turbo c++ editor
    compile it using Alt+F9
    you will get -- swap.obj

Step 4 :
    Using TLIB utility make the lib file.
    (you will find TLIB.exe in ur c:/tc/bin directory....)
    ...make sure tlib.exe and swap.obj both are in same folder....
    (if u r facing any error try to run this command in command prompt)
    for example:
TLIB swap+swap.obj
you get: swap.lib

The above process will help you to create library file n header file for ur own functions...


Using Library file & Header file in your programme
-----------------------------------------------------------------------------

Now you have to do some extra job-----
    1) Put the swap.h file in c:/tc/include directory
    2) Put the swap.lib file in c:/tc/lib directory

    Now you can create a programme to use your own library function
    for example:
/* main.c */

/*swapping using library function of swap.h*/
#include<stdio.h>
#include<conio.h>
#include<swap.h>

void main()
{
    int a,b;
    clrscr();
    printf("PLEASE ENTER FIRST  NUMBER : ");
    scanf("%d",&a);
    printf("PLEASE ENTER SECOND NUMBER : ");
    scanf("%d",&b);
    swap(a,b);
    getch();
}

    Now run your programme - Ctrl+F9
    You will see an error-- "undefined symbol _media in module uselib.c"
    This shows you hv not done your job completely, as i didn't tell u to do so.....
   
    So, let's move ahead--
    The reason behind this error is---
    you have put swap.lib n swap.h in lib and include directories respectively.
    But the complilor is unable to include the library file swap.lib automatically...
    Hence the error....
   
    Now you have the following 3 ways for running your programme........
    1) Run your programme using TCC.exe
    2) Run your programme by creating a project in C
    3) Run your programme by linking your swap.lib to Standard C library -CS.lib

   
Run your programme using TCC.exe
--------------------------------------------------------
    Again you have three options
    (all files shhould be in same folder)
    Option 1:
TCC main.c swap.c
    Option 2:
TCC main.c swap.obj
    Option 3:
TCC main.c swap.lib

    You can use any one of the three options, the result is same-
    You get the main.exe file..
    Now you can run main.exe..
    You will get the output.


Run your programme by creating a project in C
------------------------------------------------------------------------
    Open a project named 'swap.prj' in the 'project' menu using 'open project' option.
    Then insert the 'main.c' and 'swap.lib' in the project by pressing 'ins' key
    or by selecting 'project -> insert' in the menu bar.
    Then you run the 'main.c' by pressing 'Ctrl+F9' key.
    You will get the output.
    As the above method - You can use either one of the files-- 
        swap.c or  swap.obj or swap.lib


Run your programme by linking your swap.lib to Standard C library -CS.lib
-----------------------------------------------------------------------------------------------------------------
    This is the most exciting method for running your programme...
    (and of course last for me to type..)
    By using this method you will be able to use your library function
    And in the same way as you use other library functions..
    See how i made it possible.....
Step 1:
    First you have to link you file swap.lib with standard C Library cs.lib
    Using TLIB utility make the lib file.
    (you will find TLIB.exe in ur c:/tc/bin directory....)
Step 2:  
    Paste cs.lib from c:/tc/LIB/ directory to the folder where tlib.exe and swap.lib are saved.. (generally c:/tc/bin/ )
   
    ...make sure all files are in same folder....
    (if u r facing any error try to run this command in command prompt)

    for example:
    Option 1:
TLIB cs+swap.obj
    Option 2:
TLIB cs+swap.lib

    You can use any one of the two options, the result is same-
    You get : cs.lib (update version)

Step 2:
    Now put the file cs.lib in c:/tc/lib directory
    From now on you will be able yo use your new library function swap()
    and in the same way as you use other library functions...
    open main.c in c editor
    run using - Ctrl+F9
    You will get the output.
----------------------------------------------------------
Downloads---
Dwnld 1) Download example files here
Dwnld 2) If you have lost original CS.LIB you can dwnld it
-----------------------------------------------------------------------------------------------------------
            TESTED FOR C++ ALSO........
-----------------------------------------------------------------------------------------------------------
HAPPY CODING...........
ENJOY................
koolankit.mzn@gmail.com

page created by - Ankit Goel (on 25-dec-2008 at 1:00 p.m. IST)
(Last Updated on 26-dec-2008 at 12:00 a.m. IST)


Today, there have been 2 visitors (3 hits) on this page!
Page copy protected against web site content infringement by Copyscape
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free