r/C_Programming • • 23d ago

Could someone explain the practical differences between #pragma once and traditional include guards (#ifndef, #define, and #endif)?

81 Upvotes

58 comments sorted by

View all comments

Show parent comment

24

u/Maxwelldoggums 23d ago

I work professionally on a problematically large C++ project with terrible practices. Some of our headers are 10k+ lines of code, and include dozens of other headers. We actually achieved noticeably faster compile-times using ‘#pragma once’ for the reasons you outlined. Using a traditional include guard required the compiler to scan through the entire text of the header to find a matching ‘#endif’, while it can safely early-out on a pragma.

8

u/FedUp233 23d ago

Yep, can make a difference!

Don't know what compiler you are using but gcc and g++ can recognize the include guards and behave just like you used the pragma in some cases. See

https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html

https://gcc.gnu.org/onlinedocs/cpp/Once-Only-Headers.html

I think most modern compilers behave in similar fashion. Perhaps your files have something that prevents recognizing the guard macros.

Also, just as a note, the pragma does not cause the compiler to "early out" on the file when it sees the pragma in the second inclusion. In the case of the pragma or recognized include guards the header is not even opened other than the first time.

1

u/Maxwelldoggums 22d ago

Oh, good catch, thank you for the correction!

We use GCC, MSVC, and I believe Apple Clang on the same codebase depending on the target, so it’s always a gamble whether or not any particular optimization applies. In this case though, it’s likely we’re using some sort of pattern which disrupts the include guard detection. We also run a codegen pass over the files which scans for certain macros, so it may be that we’re inadvertently preventing optimization there.

2

u/ComradeGibbon 22d ago

Reminds me I'd like an abandon ship pragma.

pragma abandon(condition)

Where if the condition is true the compile just generates a empty object file.

Like

#include "project_config.h"
#pragma abandon(FEATURE_XXX == 0)  // if true stop compiling the file

2

u/FedUp233 22d ago

Is there a reason you can use #error?

2

u/ComradeGibbon 22d ago

The idea is just abandon any file that isn't supposed to be included in the build.

2

u/FedUp233 22d ago

I would think you’d want something like #error so that you’d know the build was picking up a file that was not supposed to be included in it and you could go fix the problem in make or whatever build system you were using.

Having things just disappear silently seems like a really bad idea!

Plus, then what happens at the link step when it tries to link with object files that are just not there? Or if used in a header, what about the part of the file before it hit the directive? That’s already processed and could well leave the preprocessor or compiler in an invalid state to just stop? Does it just abandon the rest of the file being included? What about #if directives that have not been closed?

1

u/ComradeGibbon 22d ago

Background in in embedded work is very common to have one code base that targets several to sometimes dozens of targets. Where you may have lots of modules that are part of the code tree but are not and cannot be built.

1

u/FedUp233 21d ago

Isn’t that what make, cmake, or whatever build system you are using is supposed to take care of depending on what target you tell it to build?

I’ve worked on large systems that had multiple targets and we never had an issue with trying to build stuff for a target that was not meant for that target. A combination of the make files and conditional compilation was maintained to take care of that.

Sorry, but a decently designed system should never get to the point of pulling in a file that was not meant for the build (as far as C and C++ or whatever source files) and for header file normal preprocessor directives should handle it fine.

Just my opinion of course.