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?

4
  • 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. Jun 7, 2019 at 21:00
  • #pragma once is not a standard feature. Jun 7, 2019 at 21:04
  • Non-standard but damn useful. Would be wise to consider adding support for it.
    – Tom
    May 2, 2022 at 6: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, 2022 at 4:27

1 Answer 1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.