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
  • So in my context i generally would not need it.

    Coming from somebody who only just understood what they were ever needed for in the first place, that's a dangerously confident generalization. For the time being, you should follow time-proven coding rules, and one of those is to treat multiple-inclusion guards like your safety belt: don't think about it, just fasten it.

    If i now include the Header File of this Lib in more than one .c File, get errors which tells me "Multiple public definitions".

    That's got nothing to do with multiple-inclusion guards. It's telling you that you have put stuff in your library's public header that doesn't belong in header files: definitions of variables. In short, some variable declarations in your header are missing the "extern" specifier.

Reply
  • So in my context i generally would not need it.

    Coming from somebody who only just understood what they were ever needed for in the first place, that's a dangerously confident generalization. For the time being, you should follow time-proven coding rules, and one of those is to treat multiple-inclusion guards like your safety belt: don't think about it, just fasten it.

    If i now include the Header File of this Lib in more than one .c File, get errors which tells me "Multiple public definitions".

    That's got nothing to do with multiple-inclusion guards. It's telling you that you have put stuff in your library's public header that doesn't belong in header files: definitions of variables. In short, some variable declarations in your header are missing the "extern" specifier.

Children