TITLE: compilation and linking with templates (Newsgroups: comp.std.c++, 18 Mar 97)a CLAMAGE: Stephen.Clamage@eng.sun.com >>|> If your compiler requires that template sources always be included at >>|> compile time, that would be the exception rather than the rule. The >>|> draft standard makes no such requirement. Indeed, there has never been >>|> such a language rule. KANZE: James Kanze >>Thank you, Steve. This is what I have been saying for a long time >>(i.e.: that compilers which required explicit inclusion of template >>implementation sources were broken according to the existing rules). CUTHBERT: dacut@henry.ece.cmu.edu (David A. Cuthbert) >Out of curiosity, how does a compiler which does not require the >inclusion of the template code know which templates to generate? >Using a specific compiler as an example, Borland 4.52 will happily >*compile* my code, but there will be a link-time error since some >template code will be missing. CLAMAGE: Stephen.Clamage@eng.sun.com (Steve Clamage) I don't know what the Borland compiler does, but I can tell you how the Sun compiler works. Let's assume you have a new program using templates which has never been compiled before. (And let's pretend there are no errors in the code, ho ho ho.) When a template needs to be instantiated, the compiler makes a note of it. At the end of the compilation it generates all needed instantiations for which source code has been seen. These in turn might require generation of still more template instantiations, and they are added to the list, extending the generation process. As each template instantiation is generated, it is added to the template data base (which used to be called a "repository"). Before generating a template instantiation, the compiler checks the data base to see whether a current version already exists -- "current" meaning not invalidated by changes to any file the instantiation depends on. (The data base includes dependency data.) If template instantiations are needed for which source code has not been seen, they are not generated at this time. ("This time" is the actual compile process of what started out as a single compilation unit. We haven't got to link time yet.) The object file will contain references to these needed templates. Eventually you link the complete program. The "link" process is actually a multi-step process which calls the actual linker as the last step. A special program scans all the object files presented for linking looking for needed template instantiations. Any current instantiations found in the template data base are automatically added to the list of files to be linked. Any missing instantiations are noted, and the template source code is compiled to create the instantiations. Those instantiations are added to the template data base and to the list of files to be linked. This process is repeated until no more instantiations are needed. The complete program is then linked. In real life, you need to recompile program parts because of errors or changes in the source code. If the changes do not invalidate an instantiation previously generated (and the compiler has to be a bit pessimistic about invalidation), that instantiation is just picked up from the data base. If you are not changing the template code, but just code that uses the templates, the previous instantiations are not invalidated, and compilation and linking goes very fast -- only the changed code is recompiled, and linking can take place directly. The compiler uses some heuristics about where to find template source code based on header file names and locations. If you choose to use conforming file names and locations, you don't have to do anything else. Otherwise, you can supply directives to the compiler about where to find the source code. Similarly, you can specify the location and use of template data bases. If you choose to use the "include everything" model of source code, all needed templates will have been instantiated by the time you get to the link phase. If you choose to use the "separate compilation" model, the first time you link the program a lot of time will be spent generating instantiations. Either way, if you don't change template code the subsequent links don't need to re-generate instantiations.