Thread overview
How are (Static) Libraries with Templates Compiled?
Mar 11, 2019
jmh530
Mar 11, 2019
ag0aep6g
Mar 11, 2019
jmh530
Mar 12, 2019
Jacob Carlborg
Mar 11, 2019
H. S. Teoh
March 11, 2019
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?
March 11, 2019
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.
March 11, 2019
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*...
March 11, 2019
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.
March 12, 2019
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