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?
Asked
Active
Viewed 6,752 times
7
Tim Long
- 305
- 3
- 11
1 Answers
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
#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 onceis not a standard feature. – AnT stands with Russia Jun 07 '19 at 21:04#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