Author Topic: Mingw/C::B compiler ignoring header wrappers to prevent multiple inclusion?  (Read 4149 times)

DJMaze

  • Guest
C::B 1.0rc2

save the following code as test.h
Code
#ifndef test_h
#define test_h

#warning test.h included

#include test.h

#endif

Now include this file somewhere in your project and compile your project.
As you can see the file gets included endlessly because it ignores the #ifndef wrapper.

Looking at the generated makefile it uses single commands for each .cxx file as well.

Isn't there a way to compile in one go to prevent the multiple inclusions?

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
No issues for me, test.h is only included once both from a command line test and as part of a C::B project.

Why aren't you using a nightly build of C::B?
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
I don't know what exactly you are doing there, but if #ifndef and #define refer to the same macro, then this certainly works fine. Very likely, you have a typo in your sources.

The code which you posted compiles fine, as is to be expected of course. This is the most basic preprocessor functionality, and it is used in every system header and every project (including Code::Blocks itself).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

DJMaze

  • Guest
maybe the sample wasn't real enough since the error actualy occures when i have 2 header files like:

/test.h
Code
#ifndef test_h
#define test_h

#warning test.h included

#include "h/test2.h"

#endif

/h/test2.h
Code
#ifndef test2_h
#define test2_h

#warning test2.h included

#include "../test.h"

#endif

Will further investigate this when i have get the nightly build.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Again, this works fine.

Header guards are one of the most basic techniques, and it is really pointless to discuss whether they work or not. They do.

If that did not work, then nobody would be able to compile anything. You cannot even include stdio.h if header guards don't work...
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."