473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 help

B2003
Dec 26 '07 #1
3 1581
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
2
by: Lokicer | last post by:
Hi! I am a newbie in C++, it seems compiler must see the source code of template classes(with .h files and .cpp files£©when compile. I want to prevent others from getting .cpp files. How should i...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
3
by: djsuson | last post by:
I'm trying to set up an inheritance tree that also uses templates. This is an attempt to simplify some previous code that was filled with redundancies. I'm working with g++ 3.4.6. The code is split...
3
by: Markus Dehmann | last post by:
I have an abstract base class which contains a function that I would like to template, but virtual template functions are illegal. I put a mini code example below, which doesn't do anything great,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.