472,958 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Template linking problem (multiple definition)

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

Jul 19 '05 #1
11 17757


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


sorry, forgot to mention that I use gcc-2.95.3 and gcc-3.2
Jul 19 '05 #2

"Georg Teichtmeister" <te***********@emt.tugraz.at> wrote in message
news:3F**************@emt.tugraz.at...
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


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

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

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

john
Jul 19 '05 #3

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

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

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


no, i write it like that:
template <typename T>
void f(T x)
{
...
}

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

Jul 19 '05 #4

<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
Jul 19 '05 #5


Agent Mulder wrote:
<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


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
Jul 19 '05 #6

"Georg Teichtmeister" <te***********@emt.tugraz.at> wrote in message
news:3F**************@emt.tugraz.at...


Agent Mulder wrote:
<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


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

I misread your post. Sorry :-(
Jul 19 '05 #7

"Georg Teichtmeister" <te***********@emt.tugraz.at> wrote in message
news:3F**************@emt.tugraz.at...

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

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

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


no, i write it like that:
template <typename T>
void f(T x)
{
...
}

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


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

john
Jul 19 '05 #8


Georg Teichtmeister wrote:
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


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

Jul 19 '05 #9


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

john

it works with my sample program, lets try it for the lib :)

thank you
Jul 19 '05 #10
>
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)
Try this

template<> inline void test<int>(int output)
{
std::cout << "Int: " << output << std::endl;
}

}


john
Jul 19 '05 #11

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:
"Georg Teichtmeister" <te***********@emt.tugraz.at> wrote in message
news:3F**************@emt.tugraz.at...
Did you inline the fully specialised functions? I.e.

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

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


Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: klaas | last post by:
Can I put implementation and header class definition in separate files also for templatized classes? I get a lot of errors and i don't know why. I make file like: gekko.h #ifndef GEKKO_h...
1
by: yanwan | last post by:
Hello I met some problems in linking a project, and hope someone can give me some advice. -----------Configuration: lighting - Win32 Release-------------------- Linking... LINK : warning...
2
by: Robbie Hatley | last post by:
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote: > Robbie Hatley wrote: > > > > I ran into a problem a few days ago when I added a couple of > > template functions to one of my personal...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
5
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ...
5
by: aryan | last post by:
Is it not allowed to have template function definition in a .cc file? Here is the scenario. The tempage function declaration is in a header file and the definition in a .cc file. The function is...
2
by: Hel | last post by:
Hi, I'm sure you are familiar with this problem: A.h: template <unsigned Nstruct A { static const unsigned a; };
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.