* Undefined reference to template function ... @ 2011-05-30 8:32 Her, Il 2011-05-31 8:53 ` Ian Lance Taylor 0 siblings, 1 reply; 8+ messages in thread From: Her, Il @ 2011-05-30 8:32 UTC (permalink / raw) To: gcc-help Hello, gcc-help. I experienced "undefined reference to template function ." when I am compiling my program with gcc 4.1.2. Almost everyone says that's because template functions weren't be implemented in the same unit where they are prototyped. (I used .cpp file for implementation and .h file for declaration). But, It works if I use gcc 3.4.6 compiler without any change of options. Why is this happening?... I am working with my customer, I can't make them understand the common rule for using template because it works on gcc 3.4.6. Can you explain this situation to me? (Something like it is changed after 4.0 or you have any options to avoid this.) Please, help me. Thank you. Her, Il Technology Consulting Technology Service Korea TEL : +82-2-2199-4475 Mobile : +82-10-4765-5597 il.her@hp.com ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Undefined reference to template function ... 2011-05-30 8:32 Undefined reference to template function Her, Il @ 2011-05-31 8:53 ` Ian Lance Taylor 2011-05-31 12:21 ` Her, Il 0 siblings, 1 reply; 8+ messages in thread From: Ian Lance Taylor @ 2011-05-31 8:53 UTC (permalink / raw) To: Her, Il; +Cc: gcc-help "Her, Il" <il.her@hp.com> writes: > I experienced "undefined reference to template function ." when I am compiling my program > with gcc 4.1.2. > > Almost everyone says > that's because template functions weren't be implemented in the same unit where they are > prototyped. (I used .cpp file for implementation and .h file for declaration). > > But, It works if I use gcc 3.4.6 compiler without any change of options. > Why is this happening?... > I am working with my customer, I can't make them understand the common rule for using template > because it works on gcc 3.4.6. > > Can you explain this situation to me? > (Something like it is changed after 4.0 or you have any options to avoid this.) It's impossible to give you a precise answer without a small example. You may find it helpful to read http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Template-Instantiation.html Ian ^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: Undefined reference to template function ... 2011-05-31 8:53 ` Ian Lance Taylor @ 2011-05-31 12:21 ` Her, Il 2011-05-31 12:44 ` Axel Freyn 2011-05-31 15:29 ` Ian Lance Taylor 0 siblings, 2 replies; 8+ messages in thread From: Her, Il @ 2011-05-31 12:21 UTC (permalink / raw) To: Ian Lance Taylor; +Cc: gcc-help 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 Please, help me to solve this. 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 2:49 PM To: Her, Il Cc: gcc-help@gcc.gnu.org Subject: Re: Undefined reference to template function ... "Her, Il" <il.her@hp.com> writes: > I experienced "undefined reference to template function ." when I am compiling my program > with gcc 4.1.2. > > Almost everyone says > that's because template functions weren't be implemented in the same unit where they are > prototyped. (I used .cpp file for implementation and .h file for declaration). > > But, It works if I use gcc 3.4.6 compiler without any change of options. > Why is this happening?... > I am working with my customer, I can't make them understand the common rule for using template > because it works on gcc 3.4.6. > > Can you explain this situation to me? > (Something like it is changed after 4.0 or you have any options to avoid this.) It's impossible to give you a precise answer without a small example. You may find it helpful to read http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Template-Instantiation.html Ian ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Undefined reference to template function ... 2011-05-31 12:21 ` Her, Il @ 2011-05-31 12:44 ` Axel Freyn 2011-05-31 15:29 ` Ian Lance Taylor 1 sibling, 0 replies; 8+ messages in thread From: Axel Freyn @ 2011-05-31 12:44 UTC (permalink / raw) To: gcc-help Hi, On Tue, May 31, 2011 at 05:59:40AM +0000, Her, Il wrote: > From: Ian Lance Taylor [mailto:iant@google.com] >> "Her, Il" <il.her@hp.com> writes: >> >>> I experienced "undefined reference to template function ." when I am >>> compiling my program with gcc 4.1.2. >>> >>> Almost everyone says that's because template functions weren't be >>> implemented in the same unit where they are prototyped. (I used .cpp >>> file for implementation and .h file for declaration). >>> >>> But, It works if I use gcc 3.4.6 compiler without any change of >>> options. Why is this happening?... >>> I am working with my customer, I can't make them understand the >>> common rule for using template because it works on gcc 3.4.6. >>> >>> Can you explain this situation to me? >>> (Something like it is changed after 4.0 or you have any options to avoid this.) >> >> It's impossible to give you a precise answer without a small example. >> >> You may find it helpful to read >> http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Template-Instantiation.html > > [...] > > ** 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 To my knowledge it's impossible. You want to compile a library which contains the "template code", and which is fully independent of the type TEST for which you instantiate the template Base<...>. If that would be possible, we could rewrite the complete C++ standard library and compile it into a shared library :-) I think, you have two ways to solve it: 1) really include also the template implementations in "sub.cpp" into main.cpp 2) Instantiate explicitely the necessary templates. In your example, you would need a line template class Base<TEST>; somewhere in a file which includes the implementations. You could e.g. add to sub.cpp the two lines (it's not necessary to include TEST.h) struct TEST; template class Base<TEST>; or generate a special "template instantiation library" (as proposed on http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Template-Instantiation.html). Axel ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Undefined reference to template function ... 2011-05-31 12:21 ` Her, Il 2011-05-31 12:44 ` Axel Freyn @ 2011-05-31 15:29 ` Ian Lance Taylor 2011-06-01 1:03 ` Her, Il 1 sibling, 1 reply; 8+ messages in thread From: Ian Lance Taylor @ 2011-05-31 15:29 UTC (permalink / raw) To: Her, Il; +Cc: gcc-help "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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: Undefined reference to template function ... 2011-05-31 15:29 ` Ian Lance Taylor @ 2011-06-01 1:03 ` Her, Il 2011-06-01 8:45 ` Axel Freyn 0 siblings, 1 reply; 8+ messages in thread From: Her, Il @ 2011-06-01 1:03 UTC (permalink / raw) To: Ian Lance Taylor; +Cc: gcc-help Yes, it is compiled well with gcc 3.4.6, but it doesn't work with gcc 4.1.2 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Undefined reference to template function ... 2011-06-01 1:03 ` Her, Il @ 2011-06-01 8:45 ` Axel Freyn 2011-06-01 13:23 ` Ian Lance Taylor 0 siblings, 1 reply; 8+ messages in thread From: Axel Freyn @ 2011-06-01 8:45 UTC (permalink / raw) To: Her, Il; +Cc: Ian Lance Taylor, gcc-help 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Undefined reference to template function ... 2011-06-01 8:45 ` Axel Freyn @ 2011-06-01 13:23 ` Ian Lance Taylor 0 siblings, 0 replies; 8+ messages in thread From: Ian Lance Taylor @ 2011-06-01 13:23 UTC (permalink / raw) To: Her, Il, Ian Lance Taylor, gcc-help On Wed, Jun 1, 2011 at 1:44 AM, Axel Freyn <axel-freyn@gmx.de> wrote: > 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 I just did the same thing on x86_64-unknown-linux-gnu, with the same results. Looking at the code, I don't see how the results could be anything else. You need to force the template expansion to occur as described on the web page referenced earlier on the thread. Also looking at the code, it did not compile, due to the extra semicolon, and I had to change main.cpp to #include "test.h" rather than "TEST.h". So I don't think you tested the exact code you showed us with gcc 3.4.6. Ian ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2011-06-01 13:23 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2011-05-30 8:32 Undefined reference to template function Her, Il 2011-05-31 8:53 ` Ian Lance Taylor 2011-05-31 12:21 ` Her, Il 2011-05-31 12:44 ` Axel Freyn 2011-05-31 15:29 ` Ian Lance Taylor 2011-06-01 1:03 ` Her, Il 2011-06-01 8:45 ` Axel Freyn 2011-06-01 13:23 ` Ian Lance Taylor
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).