Jump to content
  • Advertisement
Sign in to follow this  
wendigo23

#ifndef/#define or #pragma once?

This topic is 5437 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

Someone at my company just sent an email to everyone saying to stop using the #ifndef/#define combo to guard headers, and instead to use #pragma once because its more crap for the compiler to parse. HAHAHA right, like using a pragma vs a ifndef is going to take a lot less time to build. What do folks around here think is the better way?

Share this post


Link to post
Share on other sites
Advertisement
#pragma once ties your code to whatever compilers support this.

#ifndef works on all compilers, both C and C++, on any platform.

I'm sure the MS compiler can quickly abandon a file if it sees a #pragma once, rather than having to parse down until it finds an #endif. However, way back in the 90s compilers were bragging about parsing 1M lines of code per second. These days, I'm sure several million lines of code can be parsed per second, making this "optimization" kinda useless. Also, most header parsing is going to be OS headers, not your own... so optimizing a few of your own headers seems silly.

Correctly setting up precompiled headers will make the biggest difference. I'd opt for #ifndef because it's standard, and learning how to setup precompiled headers which will save even more parse time than #pragma once.

Share this post


Link to post
Share on other sites
I recently switched to use the #pragma once because I had some silly faults when copying classes from one header to a new one and forgetting to change the #ifdef tag. This lead to not include the new class and some searching why it is so.

But as namethatwhatever said : It ties you to the compiler which supports it and might hinder you to port your code from one platform to another. Since this doesn't bother me, I'll stick with #pragma... much less headache...

Share this post


Link to post
Share on other sites
Note that when using #pragma once, gcc 3.3 will spit out pages of "pragma once declared deprecated" warnings, or something to that effect.

I believe it was undeprecated for 3.4.

Share this post


Link to post
Share on other sites
Why not use both?
So if the compiler supports them you'll get the performance improvement while avoiding any compability issues (since unknown pragmas are ignored).

Share this post


Link to post
Share on other sites
Quote:
Original post by Namethatnobodyelsetook
I'm sure the MS compiler can quickly abandon a file if it sees a #pragma once, rather than having to parse down until it finds an #endif. However, way back in the 90s compilers were bragging about parsing 1M lines of code per second. These days, I'm sure several million lines of code can be parsed per second, making this "optimization" kinda useless. Also, most header parsing is going to be OS headers, not your own... so optimizing a few of your own headers seems silly.

I agree, it's useless in comparsion. But external includes can be almost completely removed.
By removing many external and internal dependencies and using precompiled headers we cut down your project's compilation time from minutes to seconds.

Share this post


Link to post
Share on other sites
Quote:
Original post by petewood
I think compiler writers have worked out that #ifndef and #pragma once should have the same effect and will make them equally efficient.

Yes and no. Yes they have the same effect, but #pragma once can tell the compiler to abandon what its doing instead of having to parse through the whole file to reach the #endif. But that'll make all of 1ms difference...

To the OP: Its pointless. I'd just stick to the #ifndef version because its more portable, and will cause very little speed difference.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Consider firing that guy. By suggesting that you should use pragma once instead of inclusion guards you are breaking your code. Not because the pragma isn't present on all compilers per se, it is allowed by C and C++ to have pragmas that a compiler cannot understand. However, it's stupid to have a pragma that will change the semantics of a program that way. It will effectively break your code on all other compilers. If you must use pragma once, use it together with inclusion guards. Using it should be unnecessary, though. At leas gcc (and I suspect MSVC too) has a special case to detect inclusion guards, so it probably won't slow things down at all.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous PosterAt leas gcc (and I suspect MSVC too) has a special case to detect inclusion guards, so it probably won't slow things down at all.


so how is that done then ? you have to make sure the #if/#endif encloses all of the code. you can't just check at the bottom of the file for #endif since you have no way of telling that it belongs to the #if at the top either.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!