PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > How to make template classes in .o files work?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to make template classes in .o files work?

Réponse
 
LinkBack Outils de la discussion
Vieux 26/12/2007, 11h48   #1
Boltar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to make template classes in .o files work?

Hi

I have a template class thats defined in a header file and implemented
in a .cpp module that is compiled to a .o file. No problems there. But
when I try to use this class from another .o file I get undefined
error messages from the linux linker, eg: cl_foo<int>(...) not
defined.

Presumbly this is because theres no actual instance of cl_foo<int>
created in the .o file that contains the template class. Is there a
way of getting the linker to use a bit of brains and create it on the
fly or do I have to just put the entire template class implementation
in a header file and #include it everywhere I need it (which seems a
bit archaic)?

Thanks for any

B2003
  Réponse avec citation
Vieux 26/12/2007, 11h54   #2
Rolf Magnus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to make template classes in .o files work?

Boltar wrote:

> Hi
>
> I have a template class thats defined in a header file and implemented
> in a .cpp module that is compiled to a .o file. No problems there. But
> when I try to use this class from another .o file I get undefined
> error messages from the linux linker, eg: cl_foo<int>(...) not
> defined.
>
> Presumbly this is because theres no actual instance of cl_foo<int>
> created in the .o file that contains the template class.


That's the reason.

> Is there a way of getting the linker to use a bit of brains and create it
> on the fly


No. The linker would have to see the source code and provide it to the
compiler somehow to let it generate the code. I don't know if any toolchain
does that. If by "the linux linker", you mean the GNU linker, then no, it
can't do that.

> or do I have to just put the entire template class implementation
> in a header file and #include it everywhere I need it (which seems a
> bit archaic)?


Well, you can still put the implementation in a .cpp file and then #include
that at the end of your header. Another alternative, if the set of template
instances is known in the place where the template is defined, you can use
explicit template instantiation to make the compiler generate those
template instances that you need.

  Réponse avec citation
Vieux 26/12/2007, 13h14   #3
Boltar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to make template classes in .o files work?

On 26 Dec, 11:54, Rolf Magnus <ramag...@t-online.de> wrote:
> Well, you can still put the implementation in a .cpp file and then #include
> that at the end of your header. Another alternative, if the set of template
> instances is known in the place where the template is defined, you can use
> explicit template instantiation to make the compiler generate those
> template instances that you need.


Good idea, I'll try that. Thanks.

B2003

  Réponse avec citation
Vieux 26/12/2007, 14h58   #4
Dave Rahardja
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to make template classes in .o files work?

On 2007-12-26 07:14:13 -0600, Boltar <boltar2003@yahoo.co.uk> said:

> On 26 Dec, 11:54, Rolf Magnus <ramag...@t-online.de> wrote:
>> Well, you can still put the implementation in a .cpp file and then #include
>> that at the end of your header. Another alternative, if the set of template
>> instances is known in the place where the template is defined, you can use
>> explicit template instantiation to make the compiler generate those
>> template instances that you need.

>
> Good idea, I'll try that. Thanks.


Note that explicitly instantiating those templates mean that your
library user's code can't instantiate other instances for themselves.
This may or may not be what you want.

-dr

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h40.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10114 seconds with 12 queries