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

Does the #ifndef to prevent recursive inclusion actually work ?

I am experimenting with the Keil MDK-ARM toolchain (the free version), and an STM32F407 Discovery board.
So far so good, were it not for a small problem. My background is with Embarcadero RAD Studio C++Builder, and with it I often use these starting lines in a header file :

#ifndef __THIS_HEADER
 #define __THIS_HEADER
.....
 float myarray[128];
#endif

This to prevent that including this header in more than one source file causes an
error of multiple definition of the variable myarray.

If I do the same with the uVision compiler, the linker complains loudly about
myarray being multiple defined...

Does the uVision compiler behaves differently than the RAD Studio compiler ?

Thanks,

-Fred

Parents
  • Thanks to who answered. The key phrase is the following :

    Include protection is not to protect from two source files (which normally means *.c, *.cpp) from including the same header file. It is to allow header files to include other header files and still allow another header file or the source file to also include the same header file. But multiple source files can include the same header file without include protection, since each source file is its own compilation unit.

    I thought that the include protection worked across multiple source files.
    This seems to not be the case. All clear now, thanks.

    -Fred

Reply
  • Thanks to who answered. The key phrase is the following :

    Include protection is not to protect from two source files (which normally means *.c, *.cpp) from including the same header file. It is to allow header files to include other header files and still allow another header file or the source file to also include the same header file. But multiple source files can include the same header file without include protection, since each source file is its own compilation unit.

    I thought that the include protection worked across multiple source files.
    This seems to not be the case. All clear now, thanks.

    -Fred

Children