6 ms·
The one point that is notably less relevant today is the last section regarding include files. Gcc has a special case for a header file that is entirely wrappe
by nitrogen 15y ago
The one point that is notably less relevant today is the last section regarding include files. Gcc has a special case for a header file that is entirely wrapped in an #ifndef (http://gcc.gnu.org/onlinedocs/cpp/Once_002dOnly-Headers.html#Once_002dOnly-Headers http://gcc.gnu.org/onlinedocs/cpp/Once_002dOnly-Headers.html...).
- comex 15y agoYep, which states > The result is often thousands of needless lines of code passing through the lexical analyzer, which is (in good compilers) the most expensive phase. If only...
- 16s 15y agoIs he saying that the ifndef to protect against double/multiple includes should be in the file you don't want to include multiple times? The way that part is worded seems a bit confusing.
- js2 15y agoI think he's saying this: #ifndef some_header #include some_header #endif
- apaprocki 15y agoThe more modern gcc handling is with "#pragma once": http://en.wikipedia.org/wiki/Pragma_once http://en.wikipedia.org/wiki/Pragma_once His comments on include files are not the way the world has gone. In all my years writing C, I have never seen a C/C++ header which does not include the other headers it needs in order to compile cleanly. He states: "Simple rule: include files should never include include files." If you can find me one C project out there which has headers with zero #includes and forces each compilation unit including the header to include all the prerequisites needed for that header I would be genuinely interested. Even all OS header files include their pre-requisites and it is considered a bug if they do not do so. A more modern 21st century rule would be: "Simple rule: include file order does not matter. Use include guards (or #pragma once) and always include only the prerequisites needed to cleanly compile and nothing more."
- luriel 15y ago> If you can find me one C project out there which has headers with zero #includes and forces each compilation unit including the header to include all the prerequisites needed for that header I would be genuinely interested. A whole OS does this: Plan 9 from Bell Labs :) For details see the paper by Rob Pike: How to Use the Plan 9 C Compiler http://doc.cat-v.org/plan_9/4th_edition/papers/comp http://doc.cat-v.org/plan_9/4th_edition/papers/comp Also the Plan 9 libraries are much cleaner and leaner than those in 'modern' *nix systems, which makes keeping track of includes much easier: http://man.cat-v.org/plan_9/2/intro http://man.cat-v.org/plan_9/2/intro
- apaprocki 15y agoWell it stands to reason that the Plan 9 code would work that way if Rob Pike made the comment. I'm just wondering if anyone else found this way of coding attractive. Everything I've seen points to "no".
- 4ad 15y agoDennis Ritchie, creator of C? Ken Thompson, creator of Unix?
- catwell 15y agoThere is even a relatively popular piece of software dedicated to helping developers use this style: http://code.google.com/p/include-what-you-use/ http://code.google.com/p/include-what-you-use/
- apaprocki 15y agoThat is not the stated purpose of the tool (to write code in the aforementioned style): "Include what you use" means this: for every symbol (type, function variable, or macro) that you use in foo.cc, either foo.cc or foo.h should #include a .h file that exports the declaration of that symbol. I agree 100% with that statement. According to the style, foo.h can never #include anything.