Connecting Tech Pros Worldwide Forums | Help | Site Map

Template linking problem (multiple definition)

Georg Teichtmeister
Guest
 
Posts: n/a
#1: Jul 19 '05
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


Georg Teichtmeister
Guest
 
Posts: n/a
#2: Jul 19 '05

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.
>[/color]

sorry, forgot to mention that I use gcc-2.95.3 and gcc-3.2


John Harrison
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Template linking problem (multiple definition)



"Georg Teichtmeister" <teichtmeister@emt.tugraz.at> wrote in message
news:3F5836FF.4080207@emt.tugraz.at...[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]

Did you inline the fully specialised functions? I.e.

template <typename T>
void f(T x)
{
...
}

template <>
inline void f<double>(double x)
{
...
}

john


Georg Teichtmeister
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Template linking problem (multiple definition)


[color=blue]
>
> Did you inline the fully specialised functions? I.e.
>
> template <typename T>
> void f(T x)
> {
> ...
> }
>
> template <>
> inline void f<double>(double x)
> {
> ...
> }[/color]

no, i write it like that:


template <typename T>
void f(T x)
{
...
}

template <>
void f(double x)
{
...
}

Agent Mulder
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Template linking problem (multiple definition)



<Georg Teichtmeister>
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.
<Georg Teichtmeister>

You used a 'using namespace' somewhere and now
there is ambiguity between the global names and the
(same) names used in the namespace. You can help
it by prepending :: if you want the global version.

Namespace1::ns_globalfunction(); //namespaced version used
::ns_globalfunction(); //global version called

-X


Georg Teichtmeister
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Template linking problem (multiple definition)




Agent Mulder wrote:[color=blue]
> <Georg Teichtmeister>
> 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.
> <Georg Teichtmeister>
>
> You used a 'using namespace' somewhere and now
> there is ambiguity between the global names and the
> (same) names used in the namespace. You can help
> it by prepending :: if you want the global version.
>
> Namespace1::ns_globalfunction(); //namespaced version used
> ::ns_globalfunction(); //global version called
>
> -X[/color]

sorry, i made a mistake, actually the error output is as the following:

A.lo:in function Namespace1::ns_globalfunction:
A.lo:multiple definition of Namespace1::ns_globalfunction
^^^^^^^^^^^^
B.lo:first defined here


Agent Mulder
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Template linking problem (multiple definition)



"Georg Teichtmeister" <teichtmeister@emt.tugraz.at> wrote in message
news:3F583F68.5070609@emt.tugraz.at...[color=blue]
>
>
> Agent Mulder wrote:[color=green]
> > <Georg Teichtmeister>
> > 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.
> > <Georg Teichtmeister>
> >
> > You used a 'using namespace' somewhere and now
> > there is ambiguity between the global names and the
> > (same) names used in the namespace. You can help
> > it by prepending :: if you want the global version.
> >
> > Namespace1::ns_globalfunction(); //namespaced version used
> > ::ns_globalfunction(); //global version called
> >
> > -X[/color]
>
> sorry, i made a mistake, actually the error output is as the following:
>
> A.lo:in function Namespace1::ns_globalfunction:
> A.lo:multiple definition of Namespace1::ns_globalfunction
> ^^^^^^^^^^^^
> B.lo:first defined here
>
>[/color]


I misread your post. Sorry :-(


John Harrison
Guest
 
Posts: n/a
#8: Jul 19 '05

re: Template linking problem (multiple definition)



"Georg Teichtmeister" <teichtmeister@emt.tugraz.at> wrote in message
news:3F583C5F.4050102@emt.tugraz.at...[color=blue]
>[color=green]
> >
> > Did you inline the fully specialised functions? I.e.
> >
> > template <typename T>
> > void f(T x)
> > {
> > ...
> > }
> >
> > template <>
> > inline void f<double>(double x)
> > {
> > ...
> > }[/color]
>
> no, i write it like that:
>
>
> template <typename T>
> void f(T x)
> {
> ...
> }
>
> template <>
> void f(double x)
> {
> ...
> }
>[/color]

Well that's why you get multiply defined errors. You need to inline the
fully specialised versions of your template functions.

john


Georg Teichtmeister
Guest
 
Posts: n/a
#9: Jul 19 '05

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

Georg Teichtmeister
Guest
 
Posts: n/a
#10: Jul 19 '05

re: Template linking problem (multiple definition)


[color=blue]
>
>
> Well that's why you get multiply defined errors. You need to inline the
> fully specialised versions of your template functions.
>
> john
>[/color]
it works with my sample program, lets try it for the lib :)

thank you


John Harrison
Guest
 
Posts: n/a
#11: Jul 19 '05

re: Template linking problem (multiple definition)


>[color=blue]
> 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)[/color]

Try this

template<> inline void test<int>(int output)
[color=blue]
> {
> std::cout << "Int: " << output << std::endl;
> }
>
> }[/color]

john


Geoff Macartney
Guest
 
Posts: n/a
#12: Jul 19 '05

re: Template linking problem (multiple definition)



John

why is that? what difference logically does the inline make?
Is this just a feature of gcc? or is it to do with the inline making the
definition appear only in the compilation unit it's included in?
Is this just a feature for specialised functions or does it apply to
all template functions?

Geoff



John Harrison wrote:[color=blue]
> "Georg Teichtmeister" <teichtmeister@emt.tugraz.at> wrote in message
> news:3F583C5F.4050102@emt.tugraz.at...
>[color=green][color=darkred]
>>>Did you inline the fully specialised functions? I.e.
>>>
>>>template <typename T>
>>>void f(T x)
>>>{
>>> ...
>>>}
>>>
>>>template <>
>>>inline void f<double>(double x)
>>>{
>>> ...
>>>}[/color]
>>[/color][/color]

Closed Thread