tags:

views:

417

answers:

4

Our application makes use of C++ templates in a number of places. I am currently attempting to port from gcc 3.3.3 to 4.1.0 and am encountering issues. I have recreated the problem in a small shared library and executable. I am building the share library Ok, but the executable fails to link with the following:

    undefined reference to `MyNumber<int>::~MyNumber()'
    undefined reference to `MyNumber<int>::MyNumber(int)'
    undefined reference to `MyNumber<int>::number()'

I am not using the template classes directly within the executable and would have expected this all to be encapsulated within the implementation within the shared library (probably naively so).

The problem only appears to show when working with a shared library. When I build our application on SLES 11 (gcc 4.3.2) most of my issues appear to be resolved but I still receive a number of the following:

    undefined reference to `vtable for MYCLASS<T1, T2>'

This all when linking the executable to the shared libs.

+4  A: 

It's a painfully obscure message, but it means you didn't define a virtual function, See here

Todd Gardner
A: 

Without knowing more about your source code this is very hard to answer. However asking this particular question on the gcc mailing list may yield better results, as the people who work on gcc know all the intricacies better than everyone else.

lothar
+2  A: 

I had a similar problem, and resolved it by implementating functions in the header file, not in a cpp file.

Salu2.

Miguel Angel
A: 

Thanks for the comments. I removed the -frepo and -fno-impicit-templates from my compile/link options and was good to go.

By the way, it's worth turning on all the Warning flags you can when you are writing new code. I particularly like -Wall -Werror -pedantic and, I think, -ansi. I've found this forces me to write better code and often makes porting much easier.
ChrisInEdmonton