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."

    So long as each file includes the header only once, that is exactly what you want!

    The purpose of the so-called "include guards" is to prevent the header from being included more than once in the same file!

    Essentially, the compiler only ever sees one source file at a time - that's why you need to include the necessary headers in each file, and why it isn't a problem when two different 'C' files include the same header file.

    Having said that, if your header files are faulty, they can give you problems - especially if they contain definitions of data and/or code items, rather than just declarations...

Reply
  • "There are two .c Files, which both include the same Headerfile."

    So long as each file includes the header only once, that is exactly what you want!

    The purpose of the so-called "include guards" is to prevent the header from being included more than once in the same file!

    Essentially, the compiler only ever sees one source file at a time - that's why you need to include the necessary headers in each file, and why it isn't a problem when two different 'C' files include the same header file.

    Having said that, if your header files are faulty, they can give you problems - especially if they contain definitions of data and/or code items, rather than just declarations...

Children
No data