public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Axel Freyn <axel-freyn@gmx.de>
To: gcc-help@gcc.gnu.org
Subject: Re: Undefined reference to template function ...
Date: Tue, 31 May 2011 12:44:00 -0000	[thread overview]
Message-ID: <20110531085319.GC4375@axel> (raw)
In-Reply-To: <7758AFD4380CD24F86D3266E0D083A0550544AF93B@GVW1160EXB.americas.hpqcorp.net>

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

  reply	other threads:[~2011-05-31  8:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-30  8:32 Her, Il
2011-05-31  8:53 ` Ian Lance Taylor
2011-05-31 12:21   ` Her, Il
2011-05-31 12:44     ` Axel Freyn [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110531085319.GC4375@axel \
    --to=axel-freyn@gmx.de \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).