From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21574 invoked by alias); 31 May 2011 08:53:37 -0000 Received: (qmail 21563 invoked by uid 22791); 31 May 2011 08:53:36 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mailout-de.gmx.net (HELO mailout-de.gmx.net) (213.165.64.23) by sourceware.org (qpsmtpd/0.43rc1) with SMTP; Tue, 31 May 2011 08:53:21 +0000 Received: (qmail invoked by alias); 31 May 2011 08:53:20 -0000 Received: from LN-mac29.grenoble.cnrs.fr (EHLO axel) [147.173.67.29] by mail.gmx.net (mp071) with SMTP; 31 May 2011 10:53:20 +0200 Date: Tue, 31 May 2011 12:44:00 -0000 From: Axel Freyn To: gcc-help@gcc.gnu.org Subject: Re: Undefined reference to template function ... Message-ID: <20110531085319.GC4375@axel> Mail-Followup-To: gcc-help@gcc.gnu.org References: <7758AFD4380CD24F86D3266E0D083A0550544AF1C8@GVW1160EXB.americas.hpqcorp.net> <7758AFD4380CD24F86D3266E0D083A0550544AF93B@GVW1160EXB.americas.hpqcorp.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7758AFD4380CD24F86D3266E0D083A0550544AF93B@GVW1160EXB.americas.hpqcorp.net> User-Agent: Mutt/1.5.18 (2008-05-17) X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-05/txt/msg00443.txt.bz2 Hi, On Tue, May 31, 2011 at 05:59:40AM +0000, Her, Il wrote: > From: Ian Lance Taylor [mailto:iant@google.com] >> "Her, Il" 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::dump(TEST&)` > /home/guinsa/main.cpp:11: undefined reference to `Base::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; 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; or generate a special "template instantiation library" (as proposed on http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Template-Instantiation.html). Axel