In C
, partial compilation is possible since the entire *.c
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.
In C++
it would seem that almost the same can be done - there exists a fairly uncomplicated mapping between C++
code and equivalent C
code (as far as mappings between programming languages go). However, templates seem to complicate things.
If I use, for instance, a std::vector<int>
in 1.c
, then, since the template class was specified by the <vector>
header, the compiler can generate machine code for an int
specification. Suppose in the same project there is a file 2.c
which also relies on a std::vector<int>
specialization, and that 1.o
and 2.o
must be linked. Is partial compilation of 1.c
and 2.c
to their own *.o
files to be linked later possible?
As mentioned in the linked question in the comments below, there are two commonly used methods for this problem: both generate std::vector<int>
code, or the linker goes through another round of "dependency compilations" where a single vector<int>
is compiled and then linked to both files.
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.