This is the mail archive of the gcc@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]

Which compiler flag ???


Which compiler flag should be use???

I am using Redhat-Linux version 2.91.66

$ g++ -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
 

I am getting multiple "definition error" in templates while linking all the ".o" file when i didn't use any flag.

I tried with -frpo flag but the compiler keeps on recompiling 2 or 3 files and was not able to make the shared objects.

Then i tried with -fexternal-templates, i am getting "undefined symbols" in templates while making the executables.
 
 

Now, I am checking with these three test programs.
 

// template.h

template <class T>
class TAnyClass
{
private:
        T data;
public:
        TAnyClass(const T arg) : data(arg) {};
        const T & GetData() const;
};

template <class T>
TAnyClass<T> * MakeObject(const T param);
 
 
 
 

// template.cpp

#include "template.h"
#include <string>

template <class T>
const T & TAnyClass<T>::GetData() const
{
        return data;
}

template <class T>
TAnyClass<T> * MakeObject(const T param)
{
        return new TAnyClass<T>(param);
}

/*
template class TAnyClass<int>;
template class TAnyClass<double>;
template class TAnyClass<string>;
template TAnyClass<string> * MakeObject(const string param);
*/
 
 
 

// usetemplate.cpp

#include <iostream.h>
include <string>
#include "template.h"

/*
extern template class TAnyClass<int>;
extern template class TAnyClass<double>;
extern template class TAnyClass<string>;
extern template class TAnyClass<string> * MakeObject(string param);
*/

int main()
{
        TAnyClass<int> obj1(123);
        cout << obj1.GetData() << endl;

        TAnyClass<double> obj2(3.14159);
        cout << obj2.GetData() << endl;

        TAnyClass<string> obj3("Hello");
        cout << obj3.GetData() << endl;

        string s("World");
        TAnyClass<string> *p = MakeObject(s);
        cout << p->GetData() << endl;
        delete p;
 
        return 0;
}
 
 

$ g++ -c template.cpp
$ g++ usetemplate.cpp template.o
/tmp/cc54vWyK.o: In function `main':
/tmp/cc54vWyK.o(.text+0xd): undefined reference to `TAnyClass<int>::TAnyClass(int)'
/tmp/cc54vWyK.o(.text+0x1e): undefined reference to `TAnyClass<int>::GetData(void) const'
/tmp/cc54vWyK.o(.text+0x51): undefined reference to `TAnyClass<double>::TAnyClass(double)'
/tmp/cc54vWyK.o(.text+0x62): undefined reference to `TAnyClass<double>::GetData(void) const'
/tmp/cc54vWyK.o(.text+0xa7): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >::TAnyClass(basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> >)'
/tmp/cc54vWyK.o(.text+0xb8): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >::GetData(void) const'
/tmp/cc54vWyK.o(.text+0x102): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > > * MakeObject<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >(basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> >)'
/tmp/cc54vWyK.o(.text+0x118): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >::GetData(void) const'
collect2: ld returned 1 exit status
 
 

$ g++ -c -frepo template.cpp
$ g++ -c -frepo usetemplate.cpp
$ g++ usetemplate.o template.o
usetemplate.o: In function `main':
usetemplate.o(.text+0x1e): undefined reference to `TAnyClass<int>::GetData(void) const'
usetemplate.o(.text+0x62): undefined reference to `TAnyClass<double>::GetData(void) const'
usetemplate.o(.text+0xb8): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >::GetData(void) const'
usetemplate.o(.text+0x102): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > > * MakeObject<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >(basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> >)'
usetemplate.o(.text+0x118): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >::GetData(void) const'
collect2: ld returned 1 exit status
 
 

$ g++ -c -fexternal-templates template.cpp
$ g++ -c -fexternal-templates usetemplate.cpp
template.h: In instantiation of `TAnyClass<int>::TAnyClass<int>(int)':
usetemplate.cpp:14:   instantiated from here
template.h:10: warning: template `TAnyClass<T>::TAnyClass(T)' defined in file without #pragma interface
$ g++ usetemplate.o template.o
usetemplate.o: In function `main':
usetemplate.o(.text+0x1e): undefined reference to `TAnyClass<int>::GetData(void) const'
usetemplate.o(.text+0x62): undefined reference to `TAnyClass<double>::GetData(void) const'
usetemplate.o(.text+0xb8): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >::GetData(void) const'
usetemplate.o(.text+0x102): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > > * MakeObject<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >(basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> >)'
usetemplate.o(.text+0x118): undefined reference to `TAnyClass<basic_string<char, string_char_traits<char>, __default_alloc_template<1, 0> > >::GetData(void) const'
collect2: ld returned 1 exit status
 
 

The problem of this solution is to have template instantiation in template.cpp and to use extern declaration in usetemplate.cpp. I have commented those lines.
But we are using third party tool "ospace". We can't change in ospace cpp files.

Can any one have the solution for this????

Rajiv Guliani.


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