48

Possible Duplicate:
#pragma - help understanding

I saw the pragma many times,but always confused, anyone knows what it does?Is it windows only?

Community
  • 1
  • 1
wireshark
  • 1,295
  • 2
  • 14
  • 20

4 Answers4

55

It's used to replace the following preprocessor code:

#ifndef _MYHEADER_H_
#define _MYHEADER_H_
...
#endif

A good convention is adding both to support legacy compilers (which is rare though):

#pragma once
#ifndef _MYHEADER_H_
#define _MYHEADER_H_
...
#endif

So if #pragma once fails the old method will still work.

2023 update

I see some people in the comment section advocate for using guards instead of #pragma once. This makes little to no sense in 2023 and beyond unless you are targeting some special compiler that you know does not support #pragma once.

Today's best practice is to use only #pragma once and don't bother with guards at all. Reasons being

  1. All major compilers been supporting this forever and that is not going to change.
  2. Using #pragma allows the compiler to use its internal caches which is of course faster than using the pre-processor which will always include the contents of your file just to later stumble on your guards and dismiss the whole thing.
  3. It's a lot shorter and easier to add/maintain
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Alex Kremer
  • 1,876
  • 17
  • 18
  • 6
    If the purpose is just supporting legacy preprocessors, we don't need `#pragma once`. Only the `#ifndef ... #define... #endif` will be enough for *every* preprocessor (yeah, preprocessor, not compilers, you know :) – starriet Jul 20 '22 at 16:31
  • use ```#ifndef _MYHEADER_H_ #define _MYHEADER_H_ ... #endif ``` – UNREAL Dec 18 '22 at 21:46
  • @starriet that's not entirely true as `#pragma` is a compiler directive, not a pre-processor one. you ace correct that the guards approach works with all pre-procossors tho. – Alex Kremer Jan 27 '23 at 17:14
  • The problem is that `#pragma once` can be _tricked_. While I suppose that is a nice way of finding filesystem errors in your build, the include guards cannot be tricked. And the very same machinery that implements the pragma has for just as long looked for those include guards on the first lines of your code. I see no downside to the include guards, but a singular significant one to the pragma. – Dúthomhas Jan 27 '23 at 17:33
  • 1
    @Dúthomhas can you elaborate how you can trick `#pragma once`? i'm curious. – Alex Kremer Jan 27 '23 at 18:48
  • [reddit thread on this issue](https://www.reddit.com/r/cpp/comments/a14o5q/real_world_problems_with_pragma_once/) tl;dr play with mount points and hardlinks and other filesystem goodies. – Dúthomhas Jan 27 '23 at 18:56
  • 2
    Interesting thread @Dúthomhas. my conclusion after reading the thread is - there is no problems from using `#pragma once` and the above advice stands. as people discussed in the reddit thread - if you are doing magic with hard links and other madness like that you deserve to suffer. for everyone else it works as designed :) – Alex Kremer Jan 29 '23 at 16:45
53

In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation. Thus, #pragma once serves the same purpose as #include guards, but with several advantages, including: less code, avoiding name clashes, and improved compile speed.

See the Wikipedia article for further details.

helpermethod
  • 59,493
  • 71
  • 188
  • 276
  • 1
    What does `pragma` mean?I can never remember it... – wireshark Apr 25 '11 at 09:19
  • 2
    @wireshark it's a Greek word, "πράγμα" and it comes from pragmatic as said [here](http://stackoverflow.com/questions/3791259/where-does-the-word-pragma-come-from)....Because the exact meaning in Greek is "thing", but if you focus on where it came from, you can say it means "action". – gsamaras Jul 20 '16 at 18:41
4

Generally, the #pragma directives are intended for implementing compiler-specific preprocessor instructions. They are not standardized, so you shouldn't rely on them too heavily.

In this case, #pragma once's purpose is to replace the include guards that you use in header files to avoid multiple inclusion. It works a little faster on the compilers that support it, so it may reduce the compilation time on large projects with a lot of header files that are #include'ed frequently.

  • if pragma are not standardized , what should be used then – UNREAL Dec 18 '22 at 21:44
  • @UNREAL See the above answer: https://stackoverflow.com/a/5777009/13647419 . You #define a unique name for each header file. Look up (on duckduckgo :) ) "include guards". The advantage of `#pragma once` is that it's a lot less to type, avoids accidentally using the same name for two headers, and, as mentioned above, can be a bit faster, although that doesn't matter that much. – H-005 Jan 03 '23 at 12:19
  • The problem with "unique name", of course,is that people tend to the same names... – Dúthomhas Jan 29 '23 at 21:47
3

pragma is a directive to the preprocessor. It is usually used to provide some additional control during the compilation. For example do not include the same header file code. There is a lot of different directives. The answer depends on what follows the pragma word.

danny_23
  • 503
  • 4
  • 14