r/cpp
•
u/Separate-Summer-6027
trueform.polydera.com
•
May 14 '26
•
1
Encountered a `#pragma once` failure in the wild
A header-only C++ library, headers guarded by #pragma once. We use relative includes internally. We also ship compiled WASM bindings for TypeScript.
A dockerised emscripten build on Apple Silicon under linux/amd64 emulation, the repo mounted in as a bind volume. Every translation unit reports redefinition errors: the same headers included multiple times in a single TU.
#pragma once dedupes by file identity; inode and device, in practice. On a bind mount under amd64 emulation on macOS, the same file reached through different ..-laden relative paths is not always recognised as the same file. The pragma never fires.
The problem goes away if you copy the source files into the container.
13
u/STL MSVC STL Dev May 15 '26
Moderator caution: AI-generated content is not allowed on this subreddit.
49
u/Apprehensive-Draw409 May 14 '26
That's not a compiler failure.
If your OS mirrors the same file in two locations, how is the compiler supposed to know whether you mean "just one of those two identical files" or "each of those two files just once"?
I wanted to check the standard to see how they worded it to avoid issues and.... Well, they didn't. Part of the reason is the confusion above.
82
u/sweetno May 14 '26
There are all kinds of problems when you start working with a non-local filesystem, and not only in your specific setup.
72
u/eyebrow65 May 14 '26
You might have to go old school and replace your pragma with the #ifndef…#define…#endif construct we used to use
38
u/VinnieFalco wg21.org | corosio.org May 14 '26
Pragma once, shame on you. Pragma twice, we won't be included again.
21
u/kwesoly May 14 '26
Similar rate of failure in my experience as 2 libraries using same define name in classic include guards :)
5
u/kalmoc May 14 '26
Thanks for sharing. I've looked for real-world examples for years.
19
u/BrolloksB May 14 '26
Yea, problems like this is extremely rare. Can't say the same for #define include guards though, which is why I delete every one I see and replace it with #pragma once.
2
u/GoogleIsYourFrenemy May 15 '26
Python has the same problem with its modules and paths.
2
u/cpp_is_king May 16 '26
On msvc it doesn’t even work through a symlink
-2
u/victotronics May 14 '26
Very interesting case. I guess at bottom the problem is that the C++ standard does not recognize "file" as an entity.
-11
u/mredding May 14 '26
Yep. No one is writing C++ on punch cards - saving a few characters isn't any victory. I discourage
#pragma once.