By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,217 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,217 IT Pros & Developers. It's quick & easy.

How to make template classes in .o files work?

P: n/a
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 help

B2003
Dec 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Dec 26 '07 #2

P: n/a
On 26 Dec, 11:54, Rolf Magnus <ramag...@t-online.dewrote:
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

Dec 26 '07 #3

P: n/a
On 2007-12-26 07:14:13 -0600, Boltar <bo********@yahoo.co.uksaid:
On 26 Dec, 11:54, Rolf Magnus <ramag...@t-online.dewrote:
>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

Dec 26 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.