How to create header files in C/C++ ?

First of all, does creating a header file useful at all ? If yes how ?

It obviously is or else we wouldn’t have had soemthing called a ‘header files’. 

 

Next, can we create our own header files ?

Yes, you can. It is pretty simple and its going to make your program simple and customizable.

 

Let us start with creating a header file in the traditional TurboC. Define the contents of the header file (meaning the functions you would like to include in the file) and then save it as a ‘.h’ file in the ‘include’ directory.

The path could be C:/TC/INCLUDE if you have installed Turbo C directly in ‘drive C’.

Your are done !! :) Include the header file in the program by just including the file like any other file.

#include “headerfilename.h” or #include<headerfilename.h>

 

Creating one in GCC is a bit more difficult. Define the file in the same way as stated above and save it in a ‘directory where you are going to save the program’ (NOTE: This is important. Both the header file and the program must be in the same directory, if not the program will not be able to detect your header file ). Header File successfully created ! :) But, unlike Turbo C the header file cannot be included by

#include<headerfilename.h>

The only way to include the header file is to treat the filename in the same way u treat a string.

#include “headerfilename.h”

 

Now that you have created your header files, you can create the function like sort, factorial etc. and store them in the header file. When you intend to use them, include the header file in the program and just call the function you stored in the header file.

Hasn’t id made life simple ? :D

7 Comments

  1. 1
    biswas Says:

    “Creating one in GCC is a bit more difficult” .. What is the difficulty?

    • 2
      crazycool5 Says:

      Difficult in the sense that one must ensure that the header file(created) and the program in which he has included the header file must adhere to the same folder.

  2. 3
    suraj pachkduave Says:

    sir,

    i want to creat my own header file and i want to how creat function in header file . suggest me any book by creating header file .

    also i request you to suggest topic to creat c and c++ project

    your studnet
    suraj pachkudave

    • 4

      Dude, I am student as well…. No need to call me Sir… :D

      You can create functions the same way you do it in your program. Just store it in a header file and when you want to use that funtion in your porgram, just include that header file (like you include stdio.h to use printf() ). I am not sure if you will find any books for creating header files.

      If you want information about project, follow up this site http://www.codeproject.com/

  3. 5
    Indranil Bose Says:

    As far as my knowledge is concerned, HEADER FILES contains only declaration of functions.How to implement this in GCC for self-made header files?Where does the body of the functions will reside?

  4. 6
    Indranil Bose Says:

    How to create Library file for C functions through GCC?

  5. 7

    @ Indranil Bose : No, a header file contains both the function definition and declaration. You can verify it yourself by reading any of the standard C++ header file.

    The following links are useful in library files :

    http://www.adp-gmbh.ch/cpp/gcc/create_lib.html
    http://www.network-theory.co.uk/docs/gccintro/gccintro_79.html


RSS Feed for this entry

Leave a Comment