www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - templates undefined reference

reply Christoph Singewald <christoph singewald.at> writes:
Hi!

This depends on dmd verisons:

dmd 1.0.10 (Win32)
dmd 1.0.15 (ubuntu)
dmd 1.0.16 (ubuntu)


I have a simple linked as a template and some ojects using the list
holding other objects.

e.g. List!(Obj) l = new List!(Obj);

If I comile every module individually, manually or with cmake/make, into an
object file and link these objects files afterwards then I get for every
statement constructing a list an undefined reference.

If I compile all modules within one statment togehter then evering compiles
an links perfectly.

Does somebody know a solution?
Jun 20 2007
parent reply Henning Hasemann <hhasemann web.de> writes:
Christoph Singewald <christoph singewald.at> schrieb (Wed, 20 Jun 2007
22:05:25 +0200):
 
 If I comile every module individually, manually or with cmake/make,
 into an object file and link these objects files afterwards then I
 get for every statement constructing a list an undefined reference.
 
 If I compile all modules within one statment togehter then evering
 compiles an links perfectly.
 
 Does somebody know a solution?

Try putting dummy aliases in your template module. You dont even have to use them they serve the only purpose of having the compiler instanciate the template for your types. This is not always a good idea since it makes your import graph more dense and especially more likely to contain loops, but maybe it helps. Example: class List(T) { // ... } alias List!(int) IntList; // Try to avoid this as it can lead to circular dependencies! import fooclass : Foo; alias List!(Foo) FooList; Whenever possible you should make the source of the module containing the template available at compile time, so the compiler can instanciate the template "on demand" and you dont have to define those aliases. Henning -- GPG Public Key: http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851 Fingerprint: 344F 4072 F038 BB9E B35D E6AB DDD6 D36D 4191 1851
Jun 20 2007
parent Christoph Singewald <christoph singewald.at> writes:
Hi!

Thanks for reply.
It does not mather if I add the template source at oompile time
e.g 
dmd -c list.d
dmd -c a.d list.d
dmd -c main.d

dmd .oftest main.o list.o a.o

Nothing changed, no template instance in a.o.

regarding to alias List!(int) IntList:

I have no problems with base type like (int, long, char, ...), but with
instances of classes, so if it would be a soulutin, I cant define for every
object an alias in the list.d. In that case I could dervivate an objectlist
from list for every object. 
  
christoph


Henning Hasemann wrote:

 Christoph Singewald <christoph singewald.at> schrieb (Wed, 20 Jun 2007
 22:05:25 +0200):
 
 If I comile every module individually, manually or with cmake/make,
 into an object file and link these objects files afterwards then I
 get for every statement constructing a list an undefined reference.
 
 If I compile all modules within one statment togehter then evering
 compiles an links perfectly.
 
 Does somebody know a solution?

Try putting dummy aliases in your template module. You dont even have to use them they serve the only purpose of having the compiler instanciate the template for your types. This is not always a good idea since it makes your import graph more dense and especially more likely to contain loops, but maybe it helps. Example: class List(T) { // ... } alias List!(int) IntList; // Try to avoid this as it can lead to circular dependencies! import fooclass : Foo; alias List!(Foo) FooList; Whenever possible you should make the source of the module containing the template available at compile time, so the compiler can instanciate the template "on demand" and you dont have to define those aliases. Henning

Jun 24 2007