title: How does C++ partial compilation with templates work?
body: <p>In <code>C</code>, partial compilation is possible since the entire <code>*.c</code> file can be compiled into machine code with resolution and relocation left for the linker to handle. This is just an issue of calculating the displacement certain instructions have in the final executable or knowing the absolute address for some global variable.</p>
  
  <p>In <code>C++</code> it would seem that almost the same can be done - there exists a fairly uncomplicated mapping between <code>C++</code> code and equivalent <code>C</code> code (as far as mappings between programming languages go). However, templates seem to complicate things.</p>
  
  <p>If I use, for instance, a <code>std::vector&lt;int&gt;</code> in <code>1.c</code>, then, since the template class was specified by the <code>&lt;vector&gt;</code> header, the compiler can generate machine code for an <code>int</code> specification. Suppose in the same project there is a file <code>2.c</code> which also relies on a <code>std::vector&lt;int&gt;</code> specialization, and that <code>1.o</code> and <code>2.o</code> must be linked. Is partial compilation of <code>1.c</code> and <code>2.c</code> to their own <code>*.o</code> files to be linked later possible?</p>
  
  <p>As mentioned in the linked question in the comments below, there are two commonly used methods for this problem: both generate <code>std::vector&lt;int&gt;</code> code, or the linker goes through another round of "dependency compilations" where a single <code>vector&lt;int&gt;</code> is compiled and then linked to both files.</p>
  
  <p>Regarding "greedy compilation" - does this mean that every use of template class methods in every compilation unit must be put in the linker relocation table? Also, certain calls may not use long jumps (i.e., a template class is defined right above the method using it). However, if the linker is going to force a compilation unit to use the specialization it has selected, then a long jump would be necessary - but the instruction size would be too large to patch in. </p>