templates - Templated C++ Object Files -


lets have 2 .cpp files, file1.cpp , file2.cpp, use std::vector<int>. suppose file1.cpp has int main(void). if compiled both file1.o , file2.o, , linked 2 object files elf binary can execute. compiling on 32-bit ubuntu linux machine.

my question regards how compiler , linker put symbols std::vector:

  • when linker makes final binary, there code duplication? linker have 1 set of "templated" code code in f1.o uses std::vector , set of std::vector code code comprises f2.o?

i tried myself (i used g++ -g) , looked @ final executable disassembly, , found labels generated vector constructor , other methods apparently random, although code f1.o appeared have called same constructor code f2.o. not sure, however.

if linker prevent code duplication, how it? must "know" templates are? prevent code duplication regarding multiple uses of same templated code across multiple object files?

it knows templates through name mangling. type of object encoded compiler in name, , allows linker filter out duplicate implementations of same template.

this done during linking, , not compilation, because each .o file can linked cannot stripped of may later needed. linker can decide code unused, template duplicate, etc. done using "weak symbols" in object's symbol list: symbols linker can remove if appear multiple times (as opposed other symbols, user-defined functions, cannot removed if duplicate , cause linking error).


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

c# - SharpSVN - How to get the previous revision? -

php cli reading files and how to fix it? -