Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does #pragma once mean in C? [duplicate]

Tags:

c

pragma

Possible Duplicate:
#pragma - help understanding

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

like image 652
wireshark Avatar asked Apr 25 '11 09:04

wireshark


2 Answers

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.

like image 70
helpermethod Avatar answered Sep 30 '22 22:09

helpermethod


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 tho):

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

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

like image 30
Alex Kremer Avatar answered Sep 30 '22 23:09

Alex Kremer