This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: What is wrong in this code?
- From: Ian Lance Taylor <iant at google dot com>
- To: Angelo Graziosi <graziosi dot angelo at gmail dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 9 Nov 2012 17:55:44 -0800
- Subject: Re: What is wrong in this code?
- References: <5107E401-61C5-43AE-9272-FCD4D133178E@gmail.com>
On Fri, Nov 9, 2012 at 12:34 PM, Angelo Graziosi
<graziosi.angelo@gmail.com> wrote:
>
> $ cat foo01.cc
> #include "foo.hh"
>
> MYCLASS_INSTANTIATE_TYPES
>
> $ cat foo02.cc
> #include "foo.hh"
>
> MYCLASS_INSTANTIATE_TYPES
>
> $ cat foo.hh
>
> #define MYCLASS_INSTANTIATE(g) g(int)
>
> #define MYCLASS_INSTANTIATE_BASE(type) \
> template class MyClassBase<type>;
>
> #define MYCLASS_INSTANTIATE_TYPES \
> MYCLASS_INSTANTIATE(MYCLASS_INSTANTIATE_BASE)
>
> Now what happens is this:
>
> Building on *** MAC OSX *** with gcc45, gcc46, gcc47 installed by means of MacPorts, fails as
>
> $ g++ foomain.o foo01.o foo02.o -o foo.out
> duplicate symbol MyClassBase<int>::ClassWrapper::ClassWrapper() in:
> foo01.o
> foo02.o
> duplicate symbol MyClassBase<int>::ClassWrapper::ClassWrapper() in:
> foo01.o
> foo02.o
> ld: 2 duplicate symbols for architecture x86_64
> collect2: error: ld returned 1 exit status
>
> instead it builds (on MAC OSX) using clang++,
This question is not appropriate for the mailing list gcc@gcc.gnu.org.
It would be appropriate for gcc-help@gcc.gnu.org. Please take any
followups to gcc-help. Thanks.
Instantiating a template creates all the instantiations in that
compilation unit. There is no reason to instantiate a template in
more than one compilation unit. The implementation that GCC uses does
not permit you to do that. I don't know what LLVM does.
Ian