10

Can somebody tell me a workaround for #pragma once directive support for various compilers?

I want to use in my header something like:

#if _MSC_VER > ... || __GNUC__ > ... || ...

#pragma once

#endif

Maybe it already exists in boost sources or in your code?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • To me. the big advantage of #pragma once is it gets rid of _noise_ at the beginning and end of the file. (Plus it makes auto folding of preprocessor directives more useful). The speed advantage is only relevant for compilers that haven't optimized for include guards. Once you start to add all this #if/#endif you lose the advantage and if you have to have maximum portability you should use include guards. – Tod Aug 31 '12 at 17:48

3 Answers3

20

Use include guards:

#ifndef MY_HEADER_H
#define MY_HEADER_H

// ...

#endif    // MY_HEADER_H

Sometimes you'll see these combined with the use of #pragma once:

#pragma once

#ifndef MY_HEADER_H
#define MY_HEADER_H

// ...

#endif    // MY_HEADER_H

#pragma once is pretty widely supported.

Community
  • 1
  • 1
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 1
    As documentation said, #pragma once has improved perfomance other than include guards – FrozenHeart Aug 27 '12 at 19:33
  • 1
    @NikitaTrophimov: That depends on your compiler; Some detects include guards and optimize them. And unless you compile from a floppy disk, you won't see any significant performance improvement. – BatchyX Aug 27 '12 at 19:38
  • 1
    There **is harm** in using both. `#pragma once` is compiler-specific and may cause compiling to fail on compilers that don't offer it or where it has a different meaning. Also, writing *anything* outside the include guards may prevent compilers from being able to detect and optimize them to prevent opening the same file again. You should simply never use `#pragma once`. – R.. GitHub STOP HELPING ICE Aug 28 '12 at 13:12
  • 2
    I was just wondering about the same question, and found a table that summarizes the info on `pragma once` support by different compilers: https://en.wikipedia.org/wiki/Pragma_once#Portability . It seems to be pretty safe to use it nowadays – Ivan Feb 15 '18 at 20:42
  • 1
    The question of `#pragma once` being unsupported is now relegated to: (1) embedded systems with hardware-vendor-specific compilers (and with that vendor possibly defunct), and (2) retro-computing, where code must be compiled with a toolchain more than a decade old. – rwong Apr 05 '18 at 10:48
6

#pragma once is a non-standard alternative to include guards:

#ifndef HEADER_H
#define HEADER_H

//contents of header

#endif

Both ensure the header content is not pasted more than once in the same translation unit.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 25
    Clang, g++, Intel C++, and Visual C++ all support `#pragma once`. I have yet to use a (modern) C++ compiler that doesn't support it. – James McNellis Aug 27 '12 at 19:32
  • @JamesMcNellis didn't know that, I must have had the idea because we conditionally used pragma for VC++ compilations... – Luchian Grigore Aug 27 '12 at 19:33
  • For me if using C++11 then use #pragma once – paulm Aug 07 '15 at 15:03
  • @paulm Where does C++11 to `#pragma once` relation come from? Could you provide some info/link on this? – Dmitrii Volosnykh Feb 20 '18 at 15:01
  • Was a while back so not sure, but I think because with C++11 all compilers I used supported it, where as previously only some did. – paulm Feb 21 '18 at 19:12
  • 1
    @JamesMcNellis That's interesting, however I am still curious why Mr. Stroustrup suggests not to use `pragma once` https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md – Chenna V May 08 '18 at 00:25
  • 2
    @blueskin "It is not standard and it is not portable. ... locking you down to a vendor." He's worrying about portability there. C++ is widely used. For example, some are using hardware-specific compilers for embedded systems. As Wikipedia pointed out one drawback, `#pragma once` is not perfect. But other than that it is really convenient and good, so I personally think there's no need to avoid it if your compilers all support it. – Jenix Oct 16 '19 at 06:16
1

I like to use the traditional include guards myself (as recommended by the accepted answer). It's really not that much more work, and you get the benefit of 100% portability. If you are writing a library, posting code samples, etc, it's ideal to use that old school syntax to avoid anyone else running into trouble.

That said, it has been pointed out as well by others here that the vast majority of modern compilers respect the #pragma once directive, so it's relatively improbable you will encounter an issue using it in your own projects.

Wikipedia has a list of compilers supporting the directive:

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

BuvinJ
  • 10,221
  • 5
  • 83
  • 96