www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How are (Static) Libraries with Templates Compiled?

reply jmh530 <john.michael.hall gmail.com> writes:
Suppose I have a file with a simple templated function. I compile 
that to a static library. I then compile another file that uses 
the library into an executable. When compiled to the static 
library, you don't know in advance what types it will call on the 
templated function, so that must be resolved when compiling the 
executable. So what information is in the static library that 
allows this to take place? Is the library just a simple 
collection of object files?
Mar 11 2019
next sibling parent reply ag0aep6g <anonymous example.com> writes:
On Monday, 11 March 2019 at 19:53:53 UTC, jmh530 wrote:
 So what information is in the static library that allows this 
 to take place?
None. The information is in in the source or interface file (.d/.di). Templates can't be compiled before they're instantiated.
Mar 11 2019
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Monday, 11 March 2019 at 20:11:37 UTC, ag0aep6g wrote:
 On Monday, 11 March 2019 at 19:53:53 UTC, jmh530 wrote:
 So what information is in the static library that allows this 
 to take place?
None. The information is in in the source or interface file (.d/.di). Templates can't be compiled before they're instantiated.
Ah, so you need the .lib files and the .di files to get it work.
Mar 11 2019
parent Jacob Carlborg <doob me.com> writes:
On 2019-03-11 21:59, jmh530 wrote:

 Ah, so you need the .lib files and the .di files to get it work.
You need the .di or .d files regardless if it's a template or not. Because you need to know which declarations are available. In addition to that, for templates the source (and not just the declaration) need to be present as well in the .di/.d files. -- /Jacob Carlborg
Mar 12 2019
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Mar 11, 2019 at 07:53:53PM +0000, jmh530 via Digitalmars-d-learn wrote:
 Suppose I have a file with a simple templated function. I compile that
 to a static library. I then compile another file that uses the library
 into an executable. When compiled to the static library, you don't
 know in advance what types it will call on the templated function, so
 that must be resolved when compiling the executable. So what
 information is in the static library that allows this to take place?
 Is the library just a simple collection of object files?
Templates are never compiled into any object code. Only template instantiations are. This is why you cannot elide the template body from a .di file -- because in order to instantiate the template, the compiler must know the full definition of the template. Unlike functions, where you can just declare the function signature, and the body can be an opaque binary blob that's only supplied in a precompiled object/library file. T -- Famous last words: I wonder what will happen if I do *this*...
Mar 11 2019