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.

The C++ Build Process Explained

358 pointsby green7eaover 6 years ago

11 comments

wahernover 6 years ago
&gt; You could put a function&#x27;s definition in every source file that needs it but that&#x27;s a terrible idea since the definition has to be the same everywhere if you want anything to work. Instead of having the same definition everywhere, we put the definition in a common file and include it where it is necessary. This common file is what we known as a header.<p>In C and C++ parlance &quot;definition&quot; should be &quot;declaration&quot; and &quot;implementation&quot; should be &quot;definition&quot;.[1] The terminology is important if you don&#x27;t want to get confused when learning more about C and C++. This is compounded by the fact that some languages describe these roles in the author&#x27;s original terms. (Perhaps the author&#x27;s terminology reflects his own confusion in this regard?)<p>[1] This is indisputable given the surrounding context, but I didn&#x27;t want to paste 3-4 whole paragraphs.
评论 #18457304 未加载
评论 #18457185 未加载
评论 #18457164 未加载
aidenn0over 6 years ago
If anybody is curious about how templates work, there are many ways, but the two most historically popular are:<p>Prelinker:<p>1. Compile each file, noting which templates are needed in a section in the object file<p>2. Have a special program called the &quot;prelinker&quot; run before the linker that reads each .o file, and then somehow instantiates each template (which usually, but not always requires reparsing the C++ file)<p>Weak Symbols:<p>1. When compiling instantiate every needed template, but mark it somehow in the object file as being weak, so that the linker only pulls in one instantiation for each definition.<p>The prelinker used to be more popular, as if you e.g. instantiate the same template in every single file, your compiler does <i>tons</i> more work with the weak symbols approach, <i>but</i> now weak symbols are popular both because they are <i>much</i> simpler to implement and the fact that compilation is usually parallel, while linker is typically not means that walk clock times may even be faster.
评论 #18455586 未加载
评论 #18455866 未加载
评论 #18455828 未加载
评论 #18455833 未加载
tiagomaover 6 years ago
Watch this: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=dOfucXtyEsU" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=dOfucXtyEsU</a>
ameliusover 6 years ago
Aside, C++ would have been so much nicer without the need for header files ...
评论 #18455758 未加载
评论 #18455009 未加载
评论 #18454707 未加载
评论 #18457693 未加载
评论 #18454979 未加载
crumbshotover 6 years ago
The section describing how a function call is made appears to be slightly incorrect.<p>The return value of the `add` function, in most ABI definitions, would be stored in a register. After that, the `main` function may then copy that value to its own space it has reserved on the stack.<p>This is at odds with the description in the article, which seems to describe `add` passing its return value to `main` via the stack.<p>(This is assuming no optimizations - all this would most likely be inlined anyway, with no function call.)
joker3over 6 years ago
I&#x27;ve been looking for something like this to send to some C++ newbies. This is almost what I need but not exactly. Is there something similar that explains how libraries work?
评论 #18455570 未加载
评论 #18457309 未加载
评论 #18457799 未加载
roel_vover 6 years ago
&quot;Basically, the compiler has a state which can be modified by these directives. Since every <i>.c file is treated independently, every </i>.c file that is being compiled has its own state. The headers that are included modify that file&#x27;s state. The pre-processor works at a string level and replaces the tags in the source file by the result of basic functions based on the state of the compiler.&quot;<p>I almost sort of get what the author means here but then I don&#x27;t really. I mean, there is no &#x27;state&#x27; for the compiler that is modified by precompiler directives, so this is probably an analogy or simplification he&#x27;s making here, but I don&#x27;t really understand how he gets to the mental image of &#x27;compiler state&#x27;. Why not just say it like it is: the preprocessor generates long-assed .i (or whatever) &#x27;files&#x27; in memory before the actual compiler compiles them, the content of which can be different between compilation units, because preprocessor preconditions might vary between compilation units?
评论 #18457989 未加载
marco_craveiroover 6 years ago
Bit of a side-question, but somewhat related. Is anyone working on &quot;whole program compilation&quot;? I don&#x27;t mean whole program optimisation, I mean an attempt to read <i>all</i> files for a given target in memory at the same time and then generate all translation units in one go (all in memory? and maybe linking them in memory too?). Clearly, there would be caveats (strange header inclusion techniques relying on macros to modify text of include files would break, gigantic use of memory and so forth), but for those willing to take the risk, presumably this should result in faster builds right?<p>In fact, ideally you&#x27;d even generate <i>all</i> binaries for a project in one go but that may be taking it a step too far :-)<p>At any rate, I searched google scholar for any experience reports on this and found nothing. It must have a more technical name I guess...
评论 #18458959 未加载
评论 #18460241 未加载
评论 #18459002 未加载
Const-meover 6 years ago
I don’t think that article’s accurate. At least not anymore. Modern C++ compilers do less while compiling, and much more while linking. This allows them to inline more stuff and apply some other optimizations.<p>VC++ calls that thing “Link-time Code Generation”: <a href="https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;cpp&#x2F;build&#x2F;reference&#x2F;ltcg-link-time-code-generation?view=vs-2017" rel="nofollow">https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;cpp&#x2F;build&#x2F;reference&#x2F;ltcg-li...</a><p>LLVM calls it “Link Time Optimization”, pretty similar: <a href="http:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LinkTimeOptimization.html" rel="nofollow">http:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LinkTimeOptimization.html</a>
评论 #18459395 未加载
leni536over 6 years ago
I just watched Matt Godbolt&#x27;s recent talk about the linking process[1]. It&#x27;s a pretty good talk.<p>[1] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=dOfucXtyEsU" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=dOfucXtyEsU</a>
IloveHN84over 6 years ago
I hope someday the build and linking process could be standardized, but I don&#x27;t believe it will happen, because many members of the committee come from Microsoft, Google and other tech giant who want to sell their compiler (or give it for free, but still).<p>There are too much Interests and the standardization would kill many of them