[Development] Using '#pragma once' instead of include guards?
Thiago Macieira
thiago.macieira at intel.com
Mon Oct 10 17:13:35 CEST 2022
On Monday, 10 October 2022 02:55:20 PDT Volker Hilsheimer via Development
wrote:
> Hi,
>
>
> We are using `#pragma once` in a number of examples and tests in the Qt
> source tree, but I don’t think we have officially endorsed it in favour of
> explicit include guards.
> #pragma once is “non-standard but widely supported” [1], with some caveats,
> e.g. when there are multiple header files with the same name (which each
> compiler then handles differently, as there is no standard).
> From what I see, it should in practice be ok to use. But perhaps I’m missing
> something. Does anything speak against using ‘#pragma once’ in new files?
The biggest problem we used to have was installing builds that had include
paths pointing to both the source and installation directory. With
preprocessor guards, only one of the two would actually get included; with
#pragma once, the files are actually different so both would get included,
causing a build error.
Now, with CMake I think our include paths are a lot cleaner so this may no
longer be a problem.
I should also point out that the qglobal.h include mess, which we're trying to
disentangle, may still require some guards and not #pragma once.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Cloud Software Architect - Intel DCAI Cloud Engineering
More information about the Development
mailing list