Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template class - Compilation problem

Reply
Thread Tools

Template class - Compilation problem

 
 
Marcelo
Guest
Posts: n/a
 
      01-14-2006
Hi everybody,
This is my first time with the template class and I have an strange problem. I
have spent all the afternoon trying to understand it, but I don't get the problem...


I have three files:

matrix.cpp (template class)
matrix.h
test.cpp

when I compile as:
g++ -I . *.cpp -shared -o test.so //There is no problem

if I compile as:
g++ -I . *.cpp -o test.o //There is no problem

Then i get this strange message:
g++ -I . *.cpp -o test.o
/tmp/ccrI7vlF.o: In function `main':
test.cpp.text+0x77): undefined reference to `Matrix<float>::Matrix(unsigned int)'
collect2: ld returned 1 exit status

I don't know why I get this problem. My main file is:

int main(){
Matrix<float> *vect;
(*vect) = Matrix<float>(3);
}


Can you give my some help ?
THANKS A LOT,

Marcelo

PS: I am attaching the files if someone wants to get a closer view to my
template class.

 
Reply With Quote
 
 
 
 
TB
Guest
Posts: n/a
 
      01-14-2006
Marcelo sade:
> Hi everybody,
> This is my first time with the template class and I have an strange
> problem. I have spent all the afternoon trying to understand it, but I
> don't get the problem...
>
>
> I have three files:
>
> matrix.cpp (template class)
> matrix.h
> test.cpp
>
> when I compile as:
> g++ -I . *.cpp -shared -o test.so //There is no problem
>
> if I compile as:
> g++ -I . *.cpp -o test.o //There is no problem
>
> Then i get this strange message:
> g++ -I . *.cpp -o test.o
> /tmp/ccrI7vlF.o: In function `main':
> test.cpp.text+0x77): undefined reference to
> `Matrix<float>::Matrix(unsigned int)'
> collect2: ld returned 1 exit status
>
> I don't know why I get this problem. My main file is:
>

<code snipped>

See a few hours earlier thread "template in error"
http://groups.google.com/group/comp....be3e61b3?hl=en

TB
 
Reply With Quote
 
 
 
 
Paul Henderson
Guest
Posts: n/a
 
      01-14-2006
> when I compile as:
> g++ -I . *.cpp -shared -o test.so //There is no problem
> Then i get this strange message:
> g++ -I . *.cpp -o test.o
> test.cpp.text+0x77): undefined reference to `Matrix<float>::Matrix(unsigned int)'


As the template is in a different source file from the main() function,
it is compiled separately. So, the compiler doesn't know it needs to
instantiate a Matrix<float> form of the template at the time when it is
actually building the template; hence, this is not created and you have
an unresolved external. If you compile with -shared, the linker is
happy to believe that the symbol might be resolved by linking against
something that provides it sometime later, so no error appears at this
stage, but if you compile to an executable, you're left with a missing
symbol.

To solve it, either put your template class in the same translation
unit as the stuff using it, or use an explicit instantiation in
matrix.cpp, like "template class Matrix<float>;" which will cause the
compiler to instantiate the required form. That'll also show you a
number of errors in the matrix code (which never actually got compiled
before). Finally, in your 'main', you set the value of vect, but it
isn't pointing anywhere in particular, so you'll get undefined
behaviour (probably a segfault).

 
Reply With Quote
 
Neo
Guest
Posts: n/a
 
      01-14-2006
Marcelo,

You are trying to deploy what we would ( in strict C++ ) terms call
as "Template Separation Compilation Model" i.e; to keep template
signature & actual implementation in separate files.

Since g++ still doesn't implement the "export" keyword, the only way
out is to be fall-back on "Template Inclusion Compilation Model" which
is a compile time overhead or else explicitly instantiate the class
instances from outside the main.cpp file.

You may put the same in the translation unit carrying implementation
of that Matrix constructor.

But well, of-course if you are trying to build libraries then only
way is "Template Inclusion Compilation Model"!

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-14-2006
Neo wrote:
> You are trying to deploy what we would ( in strict C++ ) terms call
> as "Template Separation Compilation Model" i.e; to keep template
> signature & actual implementation in separate files.
>
> Since g++ still doesn't implement the "export" keyword, the only way
> out is to be fall-back on "Template Inclusion Compilation Model" which
> is a compile time overhead or else explicitly instantiate the class
> instances from outside the main.cpp file.
>
> You may put the same in the translation unit carrying implementation
> of that Matrix constructor.
>
> But well, of-course if you are trying to build libraries then only
> way is "Template Inclusion Compilation Model"!


There is another option.

If the library developer knows that the template is going to be used
only for a [relatively small] limited set of template arguments, the
templates can be instantiated during building of the library by means
of _explicit_instantiation_. Simply declare (explicitly instantiate)
every possible template when compiling the library. The code will be
generated and placed in the library where later the linker will find
it while trying to resolve references in the user's code.

Look up "explicit instantiation" in the news archives, I am certain
you can find the details of what I am talking about.

V


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
problem with a function template returning a private nested class ofa class template ymost@hotmail.com C++ 2 12-28-2008 10:43 AM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments