This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

#ifndef does not work properly

Hello!

I want to include the same headerfile in more than one sourcefile and of course have to take care that each Headerfile is included just once.

I really have no clue, why this works for the Headers provided by Keil but not with my own ones:

#ifndef _MYHEADER_H_
#define _MYHEADER_H_

//Headerfile content
. ..

#endif

Obviously the preprocessor includes the Headerfile more than once and so i get errors.

When i add a statement within my Header like:

#warning "included more than once"

the warning message appears twice. (so the header is really included twice).

Can anyone tell me the problem please?!

Kind regards

Parents
  • There are two .c files, which both include the same headerfile.

    Within this headerfile i have used the #ifdef .... #endif commands to avoid that the preprocessor includes the Headerfile a second time when it was already included by the first .c file.

    This is not a problem. Like I said, each .c file needs to include the header file to see all the externals etc.

    A problem can occur when the compiler tries to include a header file twice when compiling a single .c file. Symbols will be declared twice which is an error.

    Example 1:
    header.h
    file1.c -> include header.h -> OK
    file2.c -> include header.h -> OK
    No #ifdef is needed. If you add an #ifdef in header.h and include the #warning, you will still see the warning twice because both file need to be compiled.

    Example 2:
    global.h
    header1.h -> includes global.h
    header2.h -> includes global.h as well
    file1.c -> include header1.h -> OK include header2.h -> ERROR, global.h already included
    file2.c -> include header1.h -> OK include header2.h -> ERROR, global.h already included

    Here, an #ifdef is needed for global.h

    In each example two compiles are needed, one for each .c file.

    Because it's difficult to track which header file includes which header file, it's a good idea to put the #ifdef's in all header files, just to be sure.

    What error's are you getting?

    --
    J

Reply
  • There are two .c files, which both include the same headerfile.

    Within this headerfile i have used the #ifdef .... #endif commands to avoid that the preprocessor includes the Headerfile a second time when it was already included by the first .c file.

    This is not a problem. Like I said, each .c file needs to include the header file to see all the externals etc.

    A problem can occur when the compiler tries to include a header file twice when compiling a single .c file. Symbols will be declared twice which is an error.

    Example 1:
    header.h
    file1.c -> include header.h -> OK
    file2.c -> include header.h -> OK
    No #ifdef is needed. If you add an #ifdef in header.h and include the #warning, you will still see the warning twice because both file need to be compiled.

    Example 2:
    global.h
    header1.h -> includes global.h
    header2.h -> includes global.h as well
    file1.c -> include header1.h -> OK include header2.h -> ERROR, global.h already included
    file2.c -> include header1.h -> OK include header2.h -> ERROR, global.h already included

    Here, an #ifdef is needed for global.h

    In each example two compiles are needed, one for each .c file.

    Because it's difficult to track which header file includes which header file, it's a good idea to put the #ifdef's in all header files, just to be sure.

    What error's are you getting?

    --
    J

Children
  • Example 2 should have read:

    Example 2:
    global.h
    header1.h -> includes global.h
    header2.h -> includes global.h as well
    file1.c -> include header1.h -> OK
               include header2.h -> ERROR, global.h already included
    file2.c -> include header1.h -> OK
               include header2.h -> ERROR, global.h already included
    

    Sorry about that...

    --
    J