Sign in to follow this  

HL2 SDK Header Guards

This topic is 4717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
#pragma once is even better than not having to parse the file. It doesn't even have to open the file anymore. Ie:


cl.exe a.cpp

a.cpp includes a.h twice (just as a test).

If a.h has normal include guards, the cl.exe need to reopen a.h at the second include, parse the WHOLE file, close the file.

If a.h contains the #pragma once directive at the second a.h, cl.exe knows that it's been processed before (keeps a list of processed files) and doesn't even open the file.

Be aware that some times this is very bad, if something in you .h file depends on a define (that could be set before including the .h file).
This is not that common, but it happend to me once.. (in 20 years of coding :).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If only they'd profile their compiler, they'd realise that #pragma once is actually slower than external include guards. It's not actually *that* much faster than opening the file and checking the internal guards. (Ref Compiler: Visual C++ 6, 7 & 7.1).

Share this post


Link to post
Share on other sites

This topic is 4717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this