Undefined reference to template function ...
Axel Freyn
axel-freyn@gmx.de
Wed Jun 1 08:45:00 GMT 2011
Hi
On Wed, Jun 01, 2011 at 01:03:03AM +0000, Her, Il wrote:
> Yes, it is compiled well with gcc 3.4.6,
> but it doesn't work with gcc 4.1.2
Strange -- I just tried with 3.4.6 on x86_64, configured as
../gcc-3.4.6/configure --prefix=/tmp/gcc_test/install --disable-multilib
Thread model: posix
gcc version 3.4.6
your testcase gives (after removing the erroneous ";" in sub.cpp )
/tmp/ccGJf1zv.o: In function `main':
/tmp/gcc_test/main.cpp:9: undefined reference to `Base<TEST>::dump(TEST&)'
/tmp/gcc_test/main.cpp:10: undefined reference to `Base<TEST>::fn(int, int)'
collect2: ld returned 1 exit status
(however, I did compile without the flag "-dynamic", which is only vaild
on darwin. But according to the docs, "-dynamic" is a flag for the
linker - so I don't see how it would influence the template
instantiation....)
Axel
>
>
> thank you
>
> Her, Il
> Technology Consulting
> Technology Service Korea
>
> TELÂ : +82-2-2199-4475
> Mobile : +82-10-4765-5597
> il.her@hp.com
>
> -----Original Message-----
> From: Ian Lance Taylor [mailto:iant@google.com]
> Sent: Tuesday, May 31, 2011 10:51 PM
> To: Her, Il
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: Undefined reference to template function ...
>
> "Her, Il" <il.her@hp.com> writes:
>
> > Thank you for answering my question.
> > Here is my sample.
> >
> > [Test.h]
> > #ifndef __TEST_hh
> > #define __TEST_hh
> > struct TEST
> > {
> > int a, b;
> > };
> > #endif
> >
> > [sub.h]
> > #include <stdio.h>
> > template <typename T> struct Base
> > {
> > void dump(T &r);
> > int fn(int a, int b);
> > };
> >
> > [sub.cpp]
> > #include "sub.h"
> >
> > template<typename T> void Base<T>::dump(T &r)
> > {
> > }
> >
> > template<typename T> int Base<T>::fn(int a, int b);
> > {
> > return 0;
> > }
> >
> > [main.cpp]
> > #include <stdio.h>
> > #include "sub.h"
> > #include "TEST.h"
> >
> > int main()
> > {
> > Base<TEST> t;
> > TEST x;
> >
> > t.dump(x);
> > t.fn(1, 2);
> > }
> >
> > ** Result of compiling
> > [test@test-1] $ g++ -c -dynamic sub.cpp
> > [test@test-1] $ g++ -shared -o libsub.so sub.o
> > [test@test-1] $ g++ -g -W -o main main.cpp -lsub -L./
> > /tmp/ccaUlqlQ.o: In function `main`:
> > /home/guinsa/main.cpp:10: undefined reference to `Base<TEST>::dump(TEST&)`
> > /home/guinsa/main.cpp:11: undefined reference to `Base<TEST>::fn(int, int)`
> > collect2: ld returned 1 exit status
>
>
> In your earlier message you said that you had code that worked with gcc
> 3.4.6 but failed with gcc 4.1.2. Does this test case work with gcc
> 3.4.6?
>
> Ian
More information about the Gcc-help
mailing list