This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
g++ template instantiation bug
- From: Ares Lagae <ares dot lagae at cs dot kuleuven dot ac dot be>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 14 Jul 2004 16:59:27 +0200
- Subject: g++ template instantiation bug
Sorry for emailing this directly to here, but this does not fit in a regular
gcc bug report and involves linker errors. I dont even know if this is a gcc
bug. However, this code compiles and links fine with icc.
// foo.h
#ifndef FOO
#define FOO
#include <iostream>
template <typename T>
void foo()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
template <>
void foo<int>()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
#endif
// bar1.h
#ifndef BAR1
#define BAR1
void bar1();
#endif
// bar2.h
#ifndef BAR2
#define BAR2
void bar2();
#endif
// bar1.cpp
#include "foo.h"
#include "bar1.h"
#include <iostream>
void bar1()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
// bar2.cpp
#include "foo.h"
#include "bar2.h"
#include <iostream>
void bar2()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
// main.cpp
#include "bar1.h"
#include "bar2.h"
int main(int argc, char* argv)
{
bar1();
bar2();
}
g++ bar1.cpp -c -o bar1.o
g++ bar2.cpp -c -o bar2.o
g++ main.cpp -c -o main.o
g++ bar1.o bar2.o main.o -o main
bar2.o(.text+0x0): In function `void foo<int>()':
: multiple definition of `void foo<int>()'
bar1.o(.text+0x0): first defined here
collect2: ld returned 1 exit status
gcc -v
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.3/specs
Configured with: ./configure
Thread model: posix
gcc version 3.3
ld -v
GNU ld version 2.13.90.0.18 20030121 (SuSE Linux)
when the foo<int> template specialization is removed, everything works fine.
I would appreciate some feedback if possible.
Best regards,
Ares Lagae