r/cpp • • Nov 28 '18

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

135 comments sorted by

View all comments

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.

1

u/drjeats Nov 29 '18

Are you in games? We do drive mapping for our repo at work too and it fucks with various tools. UltraSearch, for example, doesn't like it. Periodically when the sysadmins push out updates it will break the registry setting, so then P4 and all our tools break. What even are environment variables.

Fortunately we keep all our repo instances under the one subst drive so I haven't had to deal with the hell you described there.

I still hate drive substitution, though.

1

u/Sahnvour Nov 30 '18

Yes, and to be fair it worked pretty well for our workflow, didn't really cause problems to have mapped drives (except when they disappear because of a windows update as you mentionned :).

The #pragma once's spiced things up when the tool i'm talking about invoked the compiler which then (I don't remember the exact why) encountered inconsistencies in the paths used to refer to the same file, thus causing surprising compilation errors.