Linkerproblem mit dem gcc
-
Hi,
ich bekomme beim linken ein mir völlig unverständlichen Fehler, weder die Suchfunktion noch Google konnten mir bei der beseitigung helfen.
Ich weis bei diesem Fehler überhaupt nicht weiter, der Compiler läuft auch ohne zu meckern durch.Aber hier mal die Fehlermeldung:
Building target: multiFile g++ -o multiFile misc/exception.o misc/matrix.o main.o main.o: In function `main':main.cpp:(.text+0x86): undefined reference to `Matrix<short>::Matrix(unsigned short, unsigned short)' :main.cpp:(.text+0xa6): undefined reference to `Matrix<short>::setValue(unsigned short, unsigned short, short)' :main.cpp:(.text+0xc6): undefined reference to `Matrix<short>::setValue(unsigned short, unsigned short, short)' :main.cpp:(.text+0xe6): undefined reference to `Matrix<short>::setValue(unsigned short, unsigned short, short)' :main.cpp:(.text+0x106): undefined reference to `Matrix<short>::setValue(unsigned short, unsigned short, short)' :main.cpp:(.text+0x126): undefined reference to `Matrix<short>::setValue(unsigned short, unsigned short, short)' main.o:main.cpp:(.text+0x146): more undefined references to `Matrix<short>::setValue(unsigned short, unsigned short, short)' follow main.o: In function `main':main.cpp:(.text+0x15e): undefined reference to `Matrix<short>::getValue(unsigned short, unsigned short) const' :main.cpp:(.text+0x1b6): undefined reference to `Matrix<short>::getValue(unsigned short, unsigned short) const' :main.cpp:(.text+0x20e): undefined reference to `Matrix<short>::getValue(unsigned short, unsigned short) const' :main.cpp:(.text+0x266): undefined reference to `Matrix<short>::getValue(unsigned short, unsigned short) const' :main.cpp:(.text+0x2be): undefined reference to `Matrix<short>::getValue(unsigned short, unsigned short) const' main.o:main.cpp:(.text+0x316): more undefined references to `Matrix<short>::getValue(unsigned short, unsigned short) const' follow main.o: In function `main':main.cpp:(.text+0x356): undefined reference to `Matrix<short>::~Matrix()' :main.cpp:(.text+0x3f9): undefined reference to `Matrix<short>::~Matrix()' collect2: ld returned 1 exit status make: *** [multiFile] Error 1 make: Target `all' not remade because of errors. Build complete for project multiFile
Hiermal ein bißchen code:
main.cpp:
#include <iostream> #include <cstdlib> #include "misc/matrix.h" #include "misc/exception.h" using namespace std; int main() { try { Matrix<short> m(2, 3); m.setValue(0, 0, 1); m.setValue(0, 1, 2); m.setValue(0, 2, 3); m.setValue(1, 0, 4); m.setValue(1, 1, 5); m.setValue(1, 2, 6); cout << m.getValue(0, 0) << endl; cout << m.getValue(0, 1) << endl; cout << m.getValue(0, 2) << endl; cout << m.getValue(1, 0) << endl; cout << m.getValue(1, 1) << endl; cout << m.getValue(1, 2) << endl; } catch(Exception e) { cerr << e.getMsg(); exit(1); } return 0; }
matrix.h
#ifndef _MATRIX_H_ #define _MATRIX_H_ template<class T> class Matrix { private: unsigned short sizeX, sizeY; T **matrix; public: Matrix(unsigned short sizeX, unsigned short sizeY); ~Matrix(); T getValue(unsigned short x, unsigned short y) const; void setValue(unsigned short x, unsigned short y, T value); }; #endif //_MATRIX_H_
exception.h:
#ifndef _EXCEPTION_H_ #define _EXCEPTION_H_ #include <string> class Exception { private: int id; std::string msg; public: Exception(int id, std::string msg); ~Exception(); int getId() const; std::string getMsg() const; }; #endif //_EXCEPTION_H_
Die Implementierung von matrix.h und exception.h befinden sich in matrix.cpp und exception.cpp (hab ich aber mal aus platzgründen weggelassen).
Gruß Wolle.
-
Matrix ist ein Template und deshalb musst du für den GCC die definitionen der Methoden auch in die .H Datei packen, es sei denn, du willst alles explizit instanzieren.
-
Vielen Dank, daran hats gelegen.
Gruß Wolle.