473,654 Members | 3,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 templatefunctio ns 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
compilationunit s the linker gives a error which says:

A.lo:in function Namespace1::ns_ globalfunction:
A.lo:multiple definition of ns_globalfuncti on
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 17809


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 templatefunctio ns 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
compilationunit s the linker gives a error which says:

A.lo:in function Namespace1::ns_ globalfunction:
A.lo:multiple definition of ns_globalfuncti on
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.tugr az.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 templatefunctio ns 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
compilationunit s the linker gives a error which says:

A.lo:in function Namespace1::ns_ globalfunction:
A.lo:multiple definition of ns_globalfuncti on
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>(doubl e 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>(doubl e 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
compilationunit s the linker gives a error which says:

A.lo:in function Namespace1::ns_ globalfunction:
A.lo:multiple definition of ns_globalfuncti on
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_globalfunc tion(); //global version called

-X
Jul 19 '05 #5


Agent Mulder wrote:
<Georg Teichtmeister>
Now comes the problem: If I include our mathlibrary into two
compilationunit s the linker gives a error which says:

A.lo:in function Namespace1::ns_ globalfunction:
A.lo:multiple definition of ns_globalfuncti on
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_globalfunc tion(); //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.tugr az.at...


Agent Mulder wrote:
<Georg Teichtmeister>
Now comes the problem: If I include our mathlibrary into two
compilationunit s the linker gives a error which says:

A.lo:in function Namespace1::ns_ globalfunction:
A.lo:multiple definition of ns_globalfuncti on
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_globalfunc tion(); //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.tugr az.at...

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

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

template <>
inline void f<double>(doubl e 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 templatefunctio ns 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
compilationunit s the linker gives a error which says:

A.lo:in function Namespace1::ns_ globalfunction:
A.lo:multiple definition of ns_globalfuncti on
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<typena me 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<unsi gned int>(34);
}

FILE: testTemplate.cp p

#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

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

Similar topics

2
3213
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 #define GEKKO_h #include <iostream> using std::cout;
1
4364
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 LNK4075: ignoring /INCREMENTAL due to /FORCE specification LIBC.lib(crt0dat.obj) : warning LNK4006: _exit already defined in MSVCRTD.lib(MSVCRTD.dll); second definition ignored LIBC.lib(crt0dat.obj) : warning LNK4006: __exit already defined in
2
2790
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 library headers. > > > > My library, librh.a > > Linking is not defined by C++ .
5
3599
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 fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says core/profile.cc:199: error: `kNumBins' was not declared in this scope
8
7623
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 be successful but not linking. Can somebody help me in telling the exact reason why compiler cannot find the reference of template class member function definition defined in cpp file?? following problem is not linking (undefined reference of...
5
2002
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
3742
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 called in main which is in another .cc file. With g++ compiler, this fails to link.However, it compiles if the template function definitioin also included in header file. Is there a way with g++ to force template function in .cc file?
2
4571
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
391
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>
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.