Breaking

Wednesday 10 January 2018

What is Header file and Library ? How to create?



What is Header file and Library ? How to create?





Hey guys as you know c/cpp or any programming language,you might be know about header files and library files as they are required in any program.
So lets see what are this files and how to create it.

What is Header file?

Header file is a collection of the function prototypes generally built-in functions which are just declared in a file and macro definations and preprocessor commands saved with extension of .h to share within several source files.So many pre-buit header files comes with your compiler as well as you can add your own with your own purpose functions which we will see further.

How to create your own header file?

1.Create a file with extension .h and save

Type the code (Function definitions ) which you need to use in your source code.
Here we make header file with name Myheader.h

Here is example of header file code:

void add(int a, int b)
{
        printf("Addition=%d\n", a + b);
}

Put this above code in a file and save the file with name Myheader.h
That’s it now your header file is created with name Myheader.h

2. Including header file in your program

      Now as we need to include stdio.h as #include in order to use printf() function. We will also need to include the above header file myheader.h as  #include<Myhead.h>.
     Save the header file into the standard header file folder if you save the header file into current folder where the source code is saved instead of standard header files folder then you need you use as #include”Myheader.h”. The ” ” here are used to instructs the preprocessor to look into the present folder and if not found into the standard folder of all header files. So, if you wish to use angular brackets instead of ” ” to include your header file you can save it in the standard folder of header files otherwise.

3.How to use.


  Let’s see how to use.

// C program to use the above created header file

#include <stdio.h> //inbuilt header file
#include "myheader.h" //Our header file
int main()
{
     add(2, 6);
     //This calls add function written in our header file myheader.h
}

Here header file myheader is used as #include"myheader.h" as i saved the header file in the same folder where i saved the .c file. 





What is library ?

A library in C is a group of functions and declarations, exposed for use by other programs. The library therefore consists of an interface(prototype) expressed in a .h file (named the "header file") and an implementation expressed in a .c file.We saw above 
How to create header file? (with TurboC compiler) now lets see for library.

How to create your own library in c?

1.Create a .c file containing the function you want to add in library

   Create a .c file and write any funtion in th file containing the function which will get added into your library also you can add your function to existing c libraries.
   Here am taking add.c 
   
   add(int a,int b)
   {
        printf("Addition=",a+b);
   }

2.Now Compile the add.c and make add.obj (Alt+F9 for turbo c)
  
   As you compile the add.c the object file would be created with name add.obj
   then open command prompt  (Run>cmd) and type following command
   
  command >  tlib <library_name>.lib + <filename>.obj 

             C:\>tlib mathlib + c:\add.obj

  Here plus (+) sign is use to add our object file into existing math library similarly minus (-) sign is used to delete the file.

3.Now create a header file containing prototype of the function (add.c)
  
   Create a file with .h extension as we see above,but we are just adding the prototype (defination) here as the actual function is in our library.
   
   add(int a,int b);

  type above code in a file and save as myheader.h 
  myheader can be any name you want for your header file



No comments:

Post a Comment