Skip to main content
GameDev.net gamedev.net
🔒 Locked

HL2 SDK Header Guards

Started by Mr Furious Jan 16, 2005 at 2:01 PM 5 replies 750+ views
Original Post
Mr Furious
Mr Furious
I was reading through the sdk files and noticed the following at the top of the header files: #if !define( NAME_H ) #define NAME_H #ifdef _WIN32 #pragma once #endif My question is what purpose the "#pragma once" serves since there are header guard #defines in place anyway. Isn't that redundant? Does this speed up compilation somehow with Visual Studio?
Monder
Monder
You need to be able to compile the server DLL for linux if you want a dedicated linux server so I guess it's got the first header guards for GCC, why they then put #pragma once I'm not sure. I'd guess they added the first header guards after the #pragma once ones and didn't get rid of the #pramga once.
Nemesis2k2
Nemesis2k2
Yes. With a regular include guard, the preprocessor still has to scan through the rest of the file looking for the #endif. #pragma once tells it to discard the file if it's been included before, meaning when it encounters that line, it can drop that file then and there. If you have large header files, this could result in a decent speedup. Not all compilers support this pragma though (is VC++ the only one?), so a regular include guard is needed for the benefit of other compilers.
jdhardy
jdhardy
Which is odd, since GCC supports #pragma once (at least >= 3.0 does; not sure about 2.9x), it just issues a deprecation warning with versions < 3.4. In 3.4 it was un-deprecated.
Nemesis2k2
Nemesis2k2
Quote:
#pragma once is even better than not having to parse the file. It doesn't even have to open the file anymore.

Yeah, I realised that a little later. If it still had to open the file, the standard include guard would've stopped the preprocessor from encountering the #pragma once the second time though, and it really would be useless. It makes more sence if it operates when opening the file anyway.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.