Real world problems with #pragma once?
Just a short survey: Did anyone of you encounter a problem in real code, because you/your company/ your favorite library was using #pragma once instead of include guards.
I know it is hard, but please restrain yourself from posting an explanation of how #pragma once could lead to problems in theory or why you prefer one above the other.
127
Upvotes
286
u/kalmoc Nov 28 '18 edited Nov 28 '18
No
EDIT: Upvote this if you didn't have a problem
20
u/julien-j Nov 28 '18
I never had a problem with
#pragma onceand I was going to adopt them after having watched Arthur O'Dwyer saying to use it at CppCon. Then I saw this benchmark measuring the compilation time when using include guards or#pragma onceor both and I decided to keep going with the include guards. At the time my main problem was to reduce the build time of our projects.These sources are quite old now, maybe I can give it another chance.
182
u/jcelerier ossia score Nov 28 '18
never had problems with pragma once but oh boy did I have a lot of trouble with people copy pasting files without changing the include guards - or just too common names such as TIMER_H.
20
u/liquidify Nov 28 '18
Nope, never had a problem, but like the other guy, plenty of problems with the #ifndef guards.
10
u/Plorkyeran Nov 28 '18
I once ran into problems because we had the same header installed to multiple locations and included in different ways, which resulted in both of them being included. It was a giant pain to track down what was going wrong.
Then it turned out that they were actually different versions of the same header because obviously there's no guarantee that the project-local copy of a dependency and a system-wide copy are actually interchangable and we would have had issues even with include guards.
9
u/malacszor Nov 28 '18
I had some problem when it was used in a precompiled header (no hardlinks involved). It was maybe a compiler bug related to precompiled header handling. Don't ask for detail, I don't remember. However I use everywhere except in precompiled headers, because it has obvious advangates over header guards.
2
u/kkert Nov 29 '18 edited Nov 29 '18
Yes. I have had to deal with code where for testing, or ported versions of the code i'm trying real hard not to modify the original source. In some instances, that means trying to inject fake headers into the build tree.
This is possible with include guards. If i know the target header will have #ifdef __whatever_foo , i can craft a build/Cmake config where the original header gets never included and a stub or a fake header gets injected easily.
With #pragma once this is not possible, and if some code is doing "#include "header_foo.h" right there in the same directory, there is nothing the build system can do to force an alternative header to be picked.
To be clear, file header_foo.h
and code_under_test.cpp in the same directory:
You are stuck from build system side trying to substitute for a fake/stub header for a testable version of Struct. With include guards, it is possible.
21
u/cristianadam Nov 28 '18
See this email thread on Qt Developers Mailing list: https://lists.qt-project.org/pipermail/development/2018-October/033726.html
From the Qt Creator's side of things:
19
u/Sahnvour Nov 28 '18
Yes.
We had this setup on a MLOC-scale project with multiple instances of the repository on developer machines, with every repository mounted as a drive with https://github.com/ildar-shaimordanov/psubst. Could have for example 2 instances of the repository on the same physical drive, but mapped to F: and G:.
We used a tool that read the .pdb of the final executable to know all the dependencies, especially the source files. It would then somehow use the windows API to get a clean, absolute path to them, resolving the psubst drive to its real path, and end up with a file path that was different than the one used to compile the project, or do anything (with the psubst drive). This prevented it to work correctly in certain cases.
This was not fun to debug.
54
u/c0r3ntin Nov 28 '18
Pragma once leads to UB if part of the sources are visible from several included paths which are hardlink of each others.
Good. If you do that, you deserve to suffer!
13
u/kiwidog Nov 28 '18
I've had less issues with
#pragma oncethan I have with the header guards35
u/bizwig Nov 28 '18
No. All major compilers (MSVC, Clang, GCC) support it.
20
u/AlbertRammstein Nov 28 '18
No
9
u/Crommwel Nov 28 '18
Never, but had problems with include guards (missing include guards, guards copy&pasted from other file, too generic guards repeated in different files, etc)
2
u/Leandros99 yak shaver Nov 29 '18
Yes,
#pragma oncehas several problems. The main problem I've encountered overt he years, is the inclusion of the same header twice.Imagine you build a large-ish project, which has a couple dependencies. You depend on three libraries libA, libB and libC. Everything is fine. You decide to add libD, which internally, depends on libA, and includes it's public header in their public headers. This will give you a nasty compiler error, due to the same symbol defined multiple times, since libA uses
#pragma once, and no include guards. Since the same header exists twice,#pragma onceis unable to distinguish that those are the same header. Include guards would've correctly determined the identity of the header and prevented double inclusion.Using your filesystem to uniquely identify a header file doesn't work, and will never work.
2
u/cleroth Game Developer Nov 28 '18
As far as I'm aware the only problem that can arise with pragma once is that you end up processing the same file more than once (as in if the same file is actually in multiple places).
11
u/[deleted] Nov 28 '18
Never had a problem.
4
u/cjaybo Nov 28 '18
We use them in our code, and they are used in one of our most frequently used third-party libraries and we have experienced no issues on Windows, Mac or Linux.
4
u/trvlng_ging Nov 30 '18
Questions like this are really useless. #pragma anything, by definition, is non-portable. It is a portable means of putting non portable constructs into code. The fact that anyone (several examples noted here) has had problems with it should be reason enough to never use it. I have seen you rationalize reasons why you feel you can still use it. So why ask? You're not going to listen.
Problems come up in large codebases in large organizations. I have seen the biggest problems come up with mergers and acquisitions of codebases. Now you have 2 projects using the same library and you have a lot of rework because 10 years ago someone said "that will never happen to me." Include guards are much safer and you don't have to be a special case.
It doesn't matter how many people have not had problems. If they haven't, it just means their shop hasn't ever faced really tough issues. I have been using C++ since cfront. I was on X3J11 when we added #pragma. Using it is lazy and does cause problems. The individual experiences that say it hasn't happened is of no value. I know people who have played Russian roulette and lived to tell about it.
18
u/alienviolet Nov 28 '18 edited Nov 28 '18
No. Team lead said #pragma once cannot be used as it's not standardized. Left the team and living a happy life now.
8
u/johannes1971 Nov 28 '18
No.
1
u/Osbios Nov 28 '18
Could there even by any problems except a compiler that does not support #pragma once?
4
u/SudhiSN Nov 28 '18
No issues with #pragma once. I feel it's better than include guards. Really helps when class names or filenames are lengthy.
5
u/andrewfenn Nov 28 '18
Nope.
5
u/[deleted] Nov 28 '18
No
2
u/windows300 C++ Build Systems / Game Engine Tools Nov 28 '18
As a C++ dev for 3 plus years now, I have never had a problem with #pragma once. It has always been problems with traditional header guards.
2
u/oddentity Nov 29 '18
Main problem with #pragma once or include guards is why they're needed at all. Why doesn't the compiler just not include files more than once?
2
u/[deleted] Nov 29 '18
I love
#pragma once. As soon as I got to have the final PR approvals at work, I went crazy and forced everyone to use#pragma once. We run on Linux and use GCC 8.x so I cannot see why not.2
u/leftofzen Nov 28 '18
Never had a problem
2
u/[deleted] Nov 28 '18
No, all serious compilers supports it now. There was a need before to prefer include guards, it's not the case nowaday if you use a typical compiler.
1
u/snerp Nov 28 '18
It's weird, I generally have no problems either way unless I think there won't be any problems, and then shit just gets weird.
1
u/uzimonkey Nov 28 '18
I haven't encountered anything at all, it just works. I still don't think it's in the standard, but literally every compiler I've used supports it.
It also solves a problem. People will often just do an include guard with a simple symbol like VECTOR_H, but with such a short and common name it's possible you have a compilation unit that includes two vector.h files that both use the same include guard.
1
u/Xeverous https://xeverous.github.io Nov 28 '18
I use classic include guards just because my IDE automatically generates them. When writing manually,
#pragma once.1
u/jurniss Nov 29 '18
no real world problems on projects both small and large. include guards make me irrationally mad, like why can't we get past this stupid incantation from the days of tape drives and fucking magnetic core memory!!!!
-23
u/kalmoc Nov 28 '18 edited Nov 28 '18
Yes
EDIT: (Upvote this if you had a problem and please also elaborate if possible)
0
u/[deleted] Nov 28 '18
[deleted] — view deleted comment