| re: Template linking problem (multiple definition)
Georg Teichtmeister wrote:[color=blue]
> Hello!
>
> We are developing a math - library for realtime applications and want to
> use some given mathlibraries as base(ipp, MTL, .. ). Our library is a
> wrapper for those and you should be able to select the underlying
> library by template parameter. Therefore we split up our library in two
> namespaces:
>
> The first one defines namespace-global functions which call the
> baselibrary functions. These are templatefunctions which some
> spezialitions for double and float.
>
> The second one defines classes, where the functions of the first
> namespace will be called.
>
> Now comes the problem: If I include our mathlibrary into two
> compilationunits the linker gives a error which says:
>
> A.lo:in function Namespace1::ns_globalfunction:
> A.lo:multiple definition of ns_globalfunction
> B.lo:first defined here
>
> And this comes for every namespaceglobal template function of the first
> namespace for every spezialition.
>
> regards,
> Georg
>[/color]
now I have a coding example which produces the error I have:
FILE: output.h
#include <iostream>
#ifndef TEST_H
#define TEST_H
namespace Test
{
template<typename T>
void test(T output)
{
std::cout << output << std::endl;
}
template<> void test<int>(int output)
{
std::cout << "Int: " << output << std::endl;
}
}
#endif
FILE: class1.h
#include "output.h"
class class1
{
public:
class1();
};
FILE: class1.cpp
#include "class1.h"
class1::class1()
{
Test::test<int>(13);
}
FILE: class2.h
#include "class1.h"
class class2 : public class1
{
public:
class2();
};
FILE: class2.cpp
#include "class2.h"
class2::class2()
{
Test::test<unsigned int>(34);
}
FILE: testTemplate.cpp
#include "class2.h"
int main(int argc, char** argv)
{
class1 c1;
class2 c2;
}
This is the error output from the linker:
class2.o: In function `void Test::test<int>(int)':
class2.o(.text+0x0): multiple definition of `void Test::test<int>(int)'
class1.o(.text+0x0): first defined here
testTemplate.o: In function `void Test::test<int>(int)':
testTemplate.o(.text+0x0): multiple definition of `void
Test::test<int>(int)'
class1.o(.text+0x0): first defined here
collect2: ld returned 1 exit status
Compiler: gcc-2.95.3
regards,
Georg |