TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

#include <rules> (2010)

68 pointsby davikrover 1 year ago

9 comments

JonChesterfieldover 1 year ago
Good stuff, if slightly self-contradictory in places. The header file named after the source should be included first (to catch errors in the header) and the header file defining various macros should be included first (as otherwise it doesn&#x27;t work).<p>The detect-missing-includes-in-header is better handled by a separate compilation job which compiles the header by itself as it if was C++, `clang -xc foo.h`, solely for the purpose of catching that error. Then the include your own header first rule no longer matters.<p>Forward declarations have a cost which the article misses. When the thing in question changes, your compiler no longer warns you when the forward declaration is out of sync with the real thing, and the linker diagnostics are usually less comprehensible. Also it&#x27;s a nuisance to update all the forward declarations. An &lt;iosfwd&gt; style header, included at the top of the &lt;ios&gt; header, gives ~99% of the compile time advantage of forward declarations with none of the failure modes and usually less typing in the caller.
评论 #39249987 未加载
评论 #39255671 未加载
coldteaover 1 year ago
&gt;<i>We’re stuck with C++, at least for another console generation.</i><p>That was 14 years ago. C++ is not going anywhere, whether 1, 2, or 10 &quot;console generations&quot; ahead.
评论 #39250188 未加载
matheusmoreiraover 1 year ago
A great way to handle context-specific headers: common path rooted in context-specific directories coupled with include path management.<p>Useful for platform- and architecture-specific code but can be used for anything really. Anything you want to parameterize and compile conditionally.<p>Instead of this mess:<p><pre><code> #if APP_LINUX #include &lt;app&#x2F;linux.h&gt; #elif APP_BSD #include &lt;app&#x2F;bsd.h&gt; #elif APP_MACOS #include &lt;app&#x2F;macos.h&gt; #else #error &quot;Unsupported platform&quot; #endif </code></pre> Organize things like this instead:<p><pre><code> linux&#x2F;include&#x2F;app&#x2F;platform.h bsd&#x2F;include&#x2F;app&#x2F;platform.h macos&#x2F;include&#x2F;app&#x2F;platform.h </code></pre> Then in the makefile:<p><pre><code> # Detect it somehow # or have user provide it PLATFORM ?= $(shell uname -s) cc -I include -I $(PLATFORM)&#x2F;include </code></pre> Then in the source code:<p><pre><code> #include &lt;app&#x2F;platform.h&gt;</code></pre>
评论 #39250147 未加载
评论 #39250257 未加载
评论 #39250193 未加载
评论 #39250226 未加载
Hendriktoover 1 year ago
So many things to remember and potentially screw up. I am glad that modern languages come with fewer footguns.
评论 #39249896 未加载
hielkeover 1 year ago
In modern C++ there will be modules (C++20). Even std will be available as a module (C++23). This will likely bring down compile times massively. But so far the only compiler with support for this today is MSVC.
评论 #39250120 未加载
评论 #39250313 未加载
rogualover 1 year ago
I no longer use header files when I can help it. They haven&#x27;t made sense since 1980. I have a Python script that reads my C++ source and writes all the headers for me. C++ is so much nicer to write when you don&#x27;t have to do the compiler&#x27;s job for it.<p>Modules are supposed to be coming soon, and then I won&#x27;t even need the script anymore.
评论 #39258698 未加载
mgaunardover 1 year ago
this is still lacking a lot of important rules (group by nature of dependency, sort alphabetically within a group, sort groups per dependency level, use angle brackets for third-party only) and is still only barely scratching the surface of C++ file organization.
commandersakiover 1 year ago
&gt;Oh, did I mention that good header dependencies decrease the linking time?<p>How?
评论 #39250239 未加载
casey2over 1 year ago
Just don&#x27;t put include files inside other include files.