Search code examples
c++c++11compilationoperating-systempragma

c++ - Does #pragma once work on different Operating Systems?


In C++ 11, #pragma once is not part of the standard but it's widely supported. The following wikipedia link says most major compilers (GCC, Clang, etc) support it:

https://en.wikipedia.org/wiki/Pragma_once

My computer is a 64-bit Windows machine. With my compiler (GCC), #pragma once compiles. My question is whether #pragma once would also work on other major Operating Systems (Apple, Linux, and Android), given that the same compiler is used.

I'm sure there are exceptions on each OS, but I'm just wondering about whether #pragma once works on standard computers for each OS.


Solution

  • The pragma is compiler dependent, if you use a compiler with support, it will work.

    But for the moment, the usual recommendation is to use "pragma once" with headers guards.

    This manner you ensure to include the file only once, and if pragma works, the compilation time will be improved.