r/C_Programming • • 25d 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

2

u/FedUp233 24d 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 24d 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 23d 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.