Hi, why this simple codes fail to compile (error during linking)? /************************ Kelas.h **********************/ template<typename T> class Kelas { public: Kelas(); int getInt() const; }; /************************ Kelas.cpp **********************/ #include "Kelas.h" template<typename T> Kelas<T>::Kelas() { } template<typename T> int Kelas<T>::getInt() const { return 100; } /*********************** coba.cpp ***************************/ #include <iostream> #include "Kelas.h" using namespace::std; int main() { Kelas<int> k; cout << k.getInt() << endl; } /*********************** end of coba.cpp ***************************/ Compilation: 1. "g++ -c Kelas.cpp" success. 2. "g++ -c coba.cpp" success. 3. "g++ -o haha coba.o Kelas.o -L." failed: coba.o(.text+0x18): In function `main': : undefined reference to `Kelas<int>::Kelas[in-charge]()' coba.o(.text+0x32): In function `main': : undefined reference to `Kelas<int>::getInt() const' collect2: ld returned 1 exit status TIA. -- -- Verdi March --
Cincai Patron <cincaipatron@gmx.net> [Sun, 20 Jul 2003 11:42:23 +0800]:
why this simple codes fail to compile (error during linking)? /************************ Kelas.h **********************/ template<typename T> class Kelas { public: Kelas(); int getInt() const; };
/************************ Kelas.cpp **********************/ #include "Kelas.h"
template<typename T> Kelas<T>::Kelas() {
}
template<typename T> int Kelas<T>::getInt() const { return 100; }
/*********************** coba.cpp ***************************/ #include <iostream> #include "Kelas.h" using namespace::std;
int main() { Kelas<int> k; cout << k.getInt() << endl; } /*********************** end of coba.cpp ***************************/
The compiler has to see the complete definition of a template in order to instantiate it where needed (that is, until it supports the 'export' keyword). I'd therefor suggest to include Kelas.cpp in Kelas.h. Philipp -- Philipp Thomas work: pthomas@suse.de private: philipp.thomas@t-link.de
On Sunday 20 July 2003 13:26, Philipp Thomas wrote:
Cincai Patron <cincaipatron@gmx.net> [Sun, 20 Jul 2003 11:42:23 +0800]:
why this simple codes fail to compile (error during linking)? /************************ Kelas.h **********************/ template<typename T> class Kelas { public: Kelas(); int getInt() const; };
/************************ Kelas.cpp **********************/ #include "Kelas.h"
template<typename T> Kelas<T>::Kelas() {
}
template<typename T> int Kelas<T>::getInt() const { return 100; }
/*********************** coba.cpp ***************************/ #include <iostream> #include "Kelas.h" using namespace::std;
int main() { Kelas<int> k; cout << k.getInt() << endl; } /*********************** end of coba.cpp ***************************/
The compiler has to see the complete definition of a template in order to instantiate it where needed (that is, until it supports the 'export' keyword). I'd therefor suggest to include Kelas.cpp in Kelas.h.
Philipp
-- Philipp Thomas work: pthomas@suse.de private: philipp.thomas@t-link.de
Many libraries offer classes as templates, you only have to include the headers then. This should be possible here as well, what are the error messages of the linker? best, Jeroen -- Kile - a LaTeX editor for KDE http://kile.sourceforge.net
Cincai Patron <cincaipatron@gmx.net> [Sun, 20 Jul 2003 11:42:23 +0800]:
Compilation: 1. "g++ -c Kelas.cpp" success. 2. "g++ -c coba.cpp" success. 3. "g++ -o haha coba.o Kelas.o -L." failed: coba.o(.text+0x18): In function `main': : undefined reference to `Kelas<int>::Kelas[in-charge]()' coba.o(.text+0x32): In function `main': : undefined reference to `Kelas<int>::getInt() const' collect2: ld returned 1 exit status
Forget what I wrote in my last mail, this is a far easier :) The linker links in one pass, so it has to see the definition of symbols before they're referenced. In your case, try linking like this: g++ -o haha Kelas.o coba.o and it should succeed :) Philipp
On Sunday 20 July 2003 20:19, Philipp Thomas wrote:
In your case, try linking like this:
g++ -o haha Kelas.o coba.o
and it should succeed :)
Hi, I just tried your suggestion, but it still fail to link: cincai@nus-cincaipatron:/tmp> g++ -o haha Kelas.o coba.o coba.o(.text+0x18): In function `main': : undefined reference to `Kelas<int>::Kelas[in-charge]()' coba.o(.text+0x32): In function `main': : undefined reference to `Kelas<int>::getInt() const' collect2: ld returned 1 exit status OTOH, if I modified Kelas.h to: ==================== #ifndef KELAS_H #define KELAS_H class Kelas { ... }; #include Kelas.cpp #endif =================== I can compile coba.cpp with just g++ -o haha coba.cpp -- -- Verdi March --
On Mon, 21 Jul 2003 10:11:22 +0800 Verdi March <cincaipatron@gmx.net> wrote:
On Sunday 20 July 2003 20:19, Philipp Thomas wrote:
In your case, try linking like this:
g++ -o haha Kelas.o coba.o
and it should succeed :)
Hi, I just tried your suggestion, but it still fail to link:
cincai@nus-cincaipatron:/tmp> g++ -o haha Kelas.o coba.o coba.o(.text+0x18): In function `main': : undefined reference to `Kelas<int>::Kelas[in-charge]()' coba.o(.text+0x32): In function `main': : undefined reference to `Kelas<int>::getInt() const' collect2: ld returned 1 exit status
OTOH, if I modified Kelas.h to: ==================== #ifndef KELAS_H #define KELAS_H
class Kelas { ... }; #include Kelas.cpp #endif ===================
I can compile coba.cpp with just g++ -o haha coba.cpp AFAIK. The Linux linker does not support export. I've tried to build with template class member functions in a separate c++ file, and have not been able to make it work. As above, the solution is to have include everything in the header files. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
participants (5)
-
Cincai Patron
-
Jeroen Wijnhout
-
Jerry Feldman
-
Philipp Thomas
-
Verdi March