3 ms·
The standard practice of putting the ifndef guard inside each header is specifically optimized by compilers, though I don't recall where I read that.
by nitrogen 6y ago
The standard practice of putting the ifndef guard inside each header is specifically optimized by compilers, though I don't recall where I read that.
- jstimpfle 6y agoI don't know why this is downvoted. This is exactly how it is. The first time the compiler reads the include file, it remembers where the corresponding #endif is for each #if. The next time the file is included, the compiler can skip over the whole included file. There is no performance advantage from outsourcing the conditional to every including location. Putting an ifndef guard at the location where the file located is not workable. It's terrible boilerplate. To the relatively simple '#include <A.h>' another variable that must be synchronized with dependencies gets added. Plus 2-3 lines for the conditional. At each including location.
- cesarb 6y agoIIRC, this is documented in the CPP part of the GCC manual. Whenever a header file is completely enclosed in a ifndef guard, and that guard is still defined, it will skip the whole file. Another option is "#pragma once"; it's non-standard, but every popular C compiler (even MSVC) understands it, and has the advantage that there's no risk of accidentally using the same include guard for more than one header file.
- pjmlp 6y agoWindows compilers were the first to have it, naturally MSVC understands it.