Template class linkage assistance requested

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Template class linkage assistance requested

Patrick Lengel

This is a question from a first time poster.  I am trying to start working with the MinGW and MSYS environments.  I am writing a test program to test classes and am having a linkage problem when compiling classes using templates.  I am trying to create a linked list class and can compile correctly when all of my functions definitions are within a header file but when I try to split my class code across a header file and associated CPP file I run into the linkage problems.  I hope to describe my problem concisely here.  I have three files, “hello.cpp”, “sllist.h” and “sllist.cpp”.  I created a simple class print method just to test my syntax.  When the print method is in the class header everything works well because my “Hello World” program can directly see the method when the program is compiled.  When I move the test print from the header file to the CPP file the linkage fails.

 

Here are the contents:

 

hello.cpp

#include <iostream>

#include "sllist.h"

 

int main(void)

{

    SLlist<int> list;

    list.printTest(1);

    return 0;

}

 

sllist.h

#include <iostream>

template <class T>

class SLlist

{

    public:

        void printTest(int);

};

 

 

sllist.cpp

template <class T>

void SLlist<T>::printTest(int i)

{

    std::cout << "Print linked list:\n";

}

 

 

Compile Commands

g++ -c sllist.cpp

g++ -c hello.cpp

g++ sllist.o hello.o -o hello

 

 

Linkage error

hello.o:hello.cpp:(.text+0x15b): undefined reference to ‘SLlist<int>::printTest(int)’

collect2: ld returned 1 exit status

 

 

I have tried moving things around without success.  I thought it might be a linker problem so I put a normal ‘C’ function in the CPP file and added the prototype to the header file and called from my main() function and everything works properly.  Somehow I am not performing the template class syntax properly but can’t figure out what is wrong.  If it was a syntax error I would not be able to compile and run by simply moving the printTest function back into my header file.

 

I hope someone can provide some insight.  If I have committed some newbie offense please indicate what it is so that I do not commit the offense a second time.

 

Thank you,

Patrick


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Template class linkage assistance requested

John E. / TDM
Patrick Lengel wrote:

>
> This is a question from a first time poster.  I am trying to start
> working with the MinGW and MSYS environments.  I am writing a test
> program to test classes and am having a linkage problem when compiling
> classes using templates.  I am trying to create a linked list class
> and can compile correctly when all of my functions definitions are
> within a header file but when I try to split my class code across a
> header file and associated CPP file I run into the linkage problems.  
> I hope to describe my problem concisely here.  I have three files,
> “hello.cpp”, “sllist.h” and “sllist.cpp”.  I created a simple class
> print method just to test my syntax.  When the print method is in the
> class header everything works well because my “Hello World” program
> can directly see the method when the program is compiled.  When I move
> the test print from the header file to the CPP file the linkage fails.
>

(Snipped the rest.)

I recommend you read
<http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12>.

Cheers,
John E.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Template class linkage assistance requested

Greg Chicares
In reply to this post by Patrick Lengel
[To make it easier to read your message and reply to it, please:
 - post plain text, not html, and
 - break lines at some reasonable length like 72 characters.
Thanks.]

On 2008-12-14 22:39Z, Patrick Lengel wrote:
>
> This is a question from a first time poster.  I am trying to start working with the MinGW
> and MSYS environments.  I am writing a
> test program to test classes and am having a linkage problem when compiling
> classes using templates.  I am trying to
> create a linked list class and can compile correctly when all of my functions
> definitions are within a header file but when I try to split my class code
> across a header file and associated CPP file I run into the linkage
> problems.

Here's the error message, reported for your main file:

> hello.o:hello.cpp:(.text+0x15b): undefined reference to ‘SLlist<int>::printTest(int)’

and here's the definition that you put in a file by itself:

> template <class T>
>
> void SLlist<T>::printTest(int i)
>
> {
>
>     std::cout <<
> "Print linked list:\n";
>
> }

When compiling that file, gcc doesn't know you want to
instantiate SLlist<int>. When compiling your main file,
gcc knows you want SLlist<int>, but doesn't know how to
instantiate it. You could use this technique:
  http://www.google.com/search?q=explicit+instantiation
but then you'd have to add an explicit instantiation for each
type you want to use: if you later wanted SLlist<double>, then
you'd have to tell the compiler explicitly to instantiate it.
Usual practice is to keep definitions in the header instead.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Template class linkage assistance requested

Patrick Lengel
In reply to this post by John E. / TDM
Thank you for all who responded.  I am impressed with the quick and
correct feedback that was posted.  I have learned something valuable.

Kind regards,
Patrick

> Date: Sun, 14 Dec 2008 15:46:05 -0700
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [Mingw-users] Template class linkage assistance requested
>
> Patrick Lengel wrote:
> >
> > This is a question from a first time poster. I am trying to start
> > working with the MinGW and MSYS environments. I am writing a test
> > program to test classes and am having a linkage problem when compiling
> > classes using templates. I am trying to create a linked list class
> > and can compile correctly when all of my functions definitions are
> > within a header file but when I try to split my class code across a
> > header file and associated CPP file I run into the linkage problems.
> > I hope to describe my problem concisely here. I have three files,
> > “hello.cpp”, “sllist.h” and “sllist.cpp”. I created a simple class
> > print method just to test my syntax. When the print method is in the
> > class header everything works well because my “Hello World” program
> > can directly see the method when the program is compiled. When I move
> > the test print from the header file to the CPP file the linkage fails.
> >
>
> (Snipped the rest.)
>
> I recommend you read
> <http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12>.
>
> Cheers,
> John E.
>
> ------------------------------------------------------------------------------
> SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
> The future of the web can't happen without you. Join us at MIX09 to help
> pave the way to the Next Web now. Learn more and register at
> http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
>
> _______________________________________________
> This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.
Reply | Threaded
Open this post in threaded view
|

Re: Template class linkage assistance requested

Keith Marshall
On Sunday 14 December 2008 23:42:18 Patrick Lengel wrote:
> Thank you for all who responded.  I am impressed with the quick and
> correct feedback that was posted.  I have learned something
> valuable.

What a shame that you *didn't* learn not to top post, and to post in
plain text only, (*not* HTML or multipart-MIME), as Greg requested.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users

_______________________________________________
This list observes the Etiquette found at http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.