tags:

views:

106

answers:

2

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....

+8  A: 

#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 header guards.

John Calsbeek
etherything is right except warning that #pragmas are compiler extensions, they are not in Standard. You better avoid them.
f0b0s
isn't that what he said? Compiler-specific feature == non-standard compiler extension
jalf
@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?
Get the list of compiler specific pragmas? - in your compilers documentation. Best safe guard for portable code? Don't use #pragma.
Richard Corden
A: 
  • 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