37

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,188
  • 2
  • 37
  • 52
user147502
  • 978
  • 2
  • 11
  • 14

2 Answers2

39

#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
  • 20,096
  • 9
  • 75
  • 95
John Calsbeek
  • 35,189
  • 7
  • 91
  • 100
  • 1
    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
  • 1
    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
13
  • 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,850
  • 23
  • 29