This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Why do we use header files in C

Other Parts Discussed in Thread: MSP430G2553

Hi,

I am working on (MSP430G2553) Launchpad using IAR embedded workbench 5.30 on windows 7.

What exactly is the use of header files in embedded software development?

I have written a small program to blink an led on MSP430 which contain a single function void delay(void).

code is somewhat like this

main.c-->contains a single loop which calls delay() function and toggles the port

delay.c -->Contains the delay implementation ,basically a for loop-- for(i=0;i<oxffff;i++);

why do we have to put "void delay(void)" inside a header file like delay.h? why cant we just include "delay.c" directly?

I  was doing that(using delay.c without delay.h) previously and the system works ok.

And if i am  using a header file do i have to include both delay.c and delay.h in main.c as shown below

main.c-

{  

      #include "delay.h"

      #include "delay.c"

}

 

  • rahul sreedharan said:
          #include "delay.c"

    While some people include .c files into other .c files, this is not the intended use.

    Each .c file should be an independent compilation unit. That means, it will compile and will be compiled on its own.

    The header files tell other .c files about the things the associated .c file globally offers.

    So if test.c contains a function
    void test(void){ some code}
    the header file test.h will tell all other .c files that there is this function available:
    void test(void);

    Also, symbols used by the .c file are contained in the header file, so they can be used by other .c files too.

    The general rule is:

    Declarations go into the header files, definitions (the code, as well as the initialization of global variables) go into the .c file.

    And a .c file includes its own .h file, as well as those of all other .c files whose contents it needs to access.

    (note that .h files can also belong to a library, even the standard C or the runtime library)

    It is your responsibility to include all .c files into the workflow. that means making them compile and tell the linkrr to include them into the link process. When using an IDE, this is usually automatically done when adding a .c file to the project. Else it is done by adding them to the makefile script.

  • Thank you Jens-Michael Gross, I got it

  • Jens-Michael Gross said:

          #include "delay.c"

    While some people include .c files into other .c files, this is not the intended use.

    Each .c file should be an independent compilation unit. That means, it will compile and will be compiled on its own.

    The header files tell other .c files about the things the associated .c file globally offers.

    So if test.c contains a function
    void test(void){ some code}
    the header file test.h will tell all other .c files that there is this function available:
    void test(void);

    Also, symbols used by the .c file are contained in the header file, so they can be used by other .c files too.

    The general rule is:

    Declarations go into the header files, definitions (the code, as well as the initialization of global variables) go into the .c file.

    And a .c file includes its own .h file, as well as those of all other .c files whose contents it needs to access.

    (note that .h files can also belong to a library, even the standard C or the runtime library)

    It is your responsibility to include all .c files into the workflow. that means making them compile and tell the linkrr to include them into the link process. When using an IDE, this is usually automatically done when adding a .c file to the project. Else it is done by adding them to the makefile script.

    [/quote]

    Well explained! :)

**Attention** This is a public forum