Re:Notes on Programming in C (1989)
by rootbear on Sunday May 11, 2014 @09:08AM
(#7728613)
It seems to me that all this fuss over multiple includes and include guards would be solved by something like a #stop directive:
which would cause the preprocessor to skip the rest of the file. How would this not be a better solution? What am I missing?
[ Parent
| on HN ]
It would be faster, but this would still cause the compiler to open and start parsing the file unnecessarily (possibly multiple times from multiple included files).
Another approach would be to have a pragma that means: "just don't bother including this file again if you see it", and have the compiler remember it. Perhaps there is - its a long time since I programmed in C.
There is #pragma once
It's not standard, but it is supported by many compilers.
I wish for a standard #once directive. It should be very simple to implement, increase preprocessing speed and reduce the size of visually disturbing boilerplate in header files.
True, supported by GCC, Clang and Visual Studio and probably other compilers ...
Good point. Opening a file has non-trivial cost, so perhaps the added cost of burdening the compiler with avoiding needless includes is worth it. As a long time C hacker I hate to say this, but I guess the whole include mechanism is a bad design or, perhaps, a design that didn't scale well.