7

Is there any particular reason why #pragma once doesn't seem to be used in Arduino code? Is this just because it's not a well-known feature, or is there some overriding reason to use #ifndef XXX_H/#define XXX_H/#endif?

Tim Long
  • 305
  • 3
  • 11
  • Might be that part of the Arduino core source code is older than #pragma once. Which is by the way non-standard. It was introduced in GCC 3.4. – Mikael Patel Jun 07 '19 at 21:00
  • #pragma once is not a standard feature. – AnT stands with Russia Jun 07 '19 at 21:04
  • Non-standard but damn useful. Would be wise to consider adding support for it. – Tom May 02 '22 at 06:45
  • all #pragmas are non-standard by definition. However, I started using them in about 1986 in various C compilers and it seemed pretty ubiquitous even then. I guess I'm surprised that's not the case in the C++ world. – Tim Long Jul 30 '22 at 04:27

1 Answers1

9

#pragma once operates on the absolute filename of a file. Include guards work on the content of the file.

If you have multiple copies of the same library (maybe one library has some parts of another included in it - it happens...) #pragma once would have no hope of working right.

Add to that the fact that the Arduino IDE copies some files around the place when building and '#pragma once is pretty much screwed.

Include guards work regardless. So sensible people use include guards and shun #pragma once.

Majenko
  • 105,095
  • 5
  • 79
  • 137