Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to create header file ?

Status
Not open for further replies.

Parth86

Member
Hello
I have seen in many C program, there is one or more header files have in program. some have already include in compiler such as #include<regx51.h> and some have created by user such as #include<delay.h> , #include<lcd.h>

I want to understand with example. I want to create and include this header file #include <delay.h> to compiler
Code:
#include<regx51.h>          
         
#define led P2

void delay (unsigned int i)

{
unsigned int j = 0;

   for ( i = 0 ; j<1; j++);
}
void main()              
{
    while (1)                
    {                    
        led = 0x01;
        delay_ms (500);
        led = 0x01;
        delay_ms (500) ;
    }
}
I have done something to create header filer
Code:
#include<regx51.h>          
         
#include<delay.h>
#define led P2

void main()              
{
    while (1)                
    {                    
        led = 0x01;
        delay (500);
        led = 0x01;
        delay(500) ;
    }
}
 
Last edited by a moderator:
A header file is normally where you keep definitions..

Take this file
C:
#include <regx51.h>

void myFunction1(void);
void myFunction2(void);

void main(void)
   {
    myFunction1();
    myFunction2();
   }

Then do this
C:
#include <regx51.h>
#include "myHeader.h"

void main(void)
   {
    myFunction1();
    myFunction2();
   }

then make this and name it myHeader.h..
C:
#include <regx51.h>

void myFunction1(void);
void myFunction2(void);

I woulf normaly just write the functions in the header as well...
 
A header file is normally where you keep definitions..

Take this file
C:
#include <regx51.h>

void myFunction1(void);
void myFunction2(void);

void main(void)
   {
    myFunction1();
    myFunction2();
   }

Then do this
C:
#include <regx51.h>
#include "myHeader.h"

void main(void)
   {
    myFunction1();
    myFunction2();
   }

then make this and name it myHeader.h..
C:
#include <regx51.h>

void myFunction1(void);
void myFunction2(void);

I woulf normaly just write the functions in the header as well...
sorry but still have confused. I have keil compiler
step 1 : we are creating header file (given name myHeader.h and saved it )
step 2. we write C program (given name hello .c )
can you explain for header file #include<delay.h>?
 
Look at the line... #include<delay.h>

The include is within <> brackets, this means all the functions within the header are already within the object file the compiler uses to build an exacutable... If you use any of the functions in the delay.h then they will be included in the build.. If you look into the header file ( it will be in the compiler directory under include ) you will see a list of functions and types that are available..

If the line was .. #include "delay.h" Quotation marks... It needs to be in the working directory.. This will be source code that must be manually included in the build..

So in short! If you create any headers, you will write #include "myheader.h".. If the header has come with the compiler then you will write #include <yourHeader.h>...

The best example is.. #include <regx51>.. This is written by the keil compiler guys and resides within their directory structure..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top