This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Linking issue while using C++ Templates


More precisely, something like this,

$ g++ x.cpp
/tmp/ccA65pR9.o: In function `A<B>::getInstance(void)':
/tmp/ccA65pR9.o(.A<B>::gnu.linkonce.t.getInstance(void)+0x7): undefined reference to `A<B>::_instance'
/tmp/ccA65pR9.o(.A<B>::gnu.linkonce.t.getInstance(void)+0x36): undefined reference to `A<B>::_instance'
/tmp/ccA65pR9.o(.A<B>::gnu.linkonce.t.getInstance(void)+0x3c): undefined reference to `A<B>::_instance'
collect2: ld returned 1 exit status


Thanks,
Rajat.



----- Original Message ----
From: Rajat <myself_rajat@yahoo.com>
To: gcc-help@gcc.gnu.org
Sent: Wednesday, September 23, 2009 10:17:07 AM
Subject: Linking issue while using C++ Templates

Hi All,

I am using C++ templates, and facing some strange linking issues with GCC 3.4.6. Here it goes,

I have a singalton class template, say A.h .
//A.h -- sample code.
template<typename T> class A
{
public:
    static A * getInstance ()
    {
        if (!_instance)
            _instance = new A ();
        return _instance;
    }

T * getApp () const { return _pApp; }

private:
    T * _pApp;
    static A * _instance;
};


Another class B, as B.h,

//B.h
class B : public C
{
public:
//Some var and funcs are defined.
};


Now I am trying to use it in a .cpp file as,
//x.cpp
#include "A.h"
#include "B.h"
--------------------
--------------------
B * app = A<B>::getInstance ()->getApp ();
--------------------
--------------------


File x.cpp compiles perfactly, but while linking it throws error saying,
x.o(.gnu.linkonce.t._ZN9AI11BE11getInstanceEv+0x9): In function `A<B>::getInstance()':
A.h:: undefined reference to `A<B>::_instance'
collect2: ld returned 1 exit status

Any clue ?

Thanks,
Rajat.


      


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]