41

Can anyone help me understand #pragma?

ifndef TARGET_OS_LINUX
#pragma once
endif

What,when, where, why, an example?

The above is in some code that I am refactoring....

JabberwockyDecompiler
  • 3,318
  • 2
  • 42
  • 54
user147502
  • 1,038
  • 2
  • 12
  • 14

2 Answers2

45

#pragma is just the prefix for a compiler-specific feature.

In this case, #pragma once means that this header file will only ever be included once in a specific destination file. It removes the need for include guards.

Mobiletainment
  • 22,201
  • 9
  • 82
  • 98
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • 2
    etherything is right except warning that #pragmas are compiler extensions, they are not in Standard. You better avoid them. – f0b0s Aug 11 '09 at 23:42
  • 2
    isn't that what he said? Compiler-specific feature == non-standard compiler extension – jalf Aug 12 '09 at 00:01
  • @John - So where can I get a list of compiler specific features that I can use? These change depending upon GCC and Intel CC I am sure, so what is the best safe guard for portable code? – user147502 Aug 12 '09 at 02:50
  • 1
    Get the list of compiler specific pragmas? - in your compilers documentation. Best safe guard for portable code? Don't use #pragma. – Richard Corden Aug 12 '09 at 07:59
  • Included in this answer should be what the word "pragma" actually stands for or is short for! – jbyrd Sep 29 '17 at 18:04
15
  • What -- it is header guard. This file will be included only once.
  • When -- at a compile process
  • why -- to avoid double including.

"Header guards are little pieces of code that protect the contents of a header file from being included more than once."

f0b0s
  • 2,978
  • 26
  • 30