I just installed MinGW and Eclipse and tried to program a very simple Demo, I jst used the Project for Building an executable and added two files for a Class. It acturally generates two objects, text.o(the function with main) and the MyClass.o. But I get the Error, there seems something wrong with the linking I guess:
12:23:24 **** Incremental Build of configuration Debug for project test **** Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\MyClass.o" "..\src\MyClass.cpp"
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\test.o" "..\src\test.cpp"
g++ -o test.exe "src\test.o" "src\MyClass.o"
src\MyClass.o:MyClass.cpp:(.rdata$.refptr._ZN7myClass13myInternalVarE[.refptr._ZN7myClass13myInternalVarE]+0x0): undefined reference to `myClass::myInternalVar'
collect2.exe: error: ld returned 1 exit status
MyClass Headerfile:
#ifndef MYCLASS_HPP_
#define MYCLASS_HPP_
class myClass{
private:
static int myInternalVar;
protected:
public:
myClass();
myClass(int initVal);
~myClass();
void set(int value);
int get();
};
#endif /* MYCLASS_HPP_ */
MyClass Sourcefile:
#include "MyClass.hpp"
myClass::myClass()
{
}
myClass::myClass(int initVal)
{
}
myClass::~myClass()
{
}
void myClass::set(int value)
{
myClass::myInternalVar = value;
}
int myClass::get()
{
return myClass::myInternalVar;
}
Test Source:
using namespace std;
#include <iostream>
#include "MyClass.hpp"
int main() {
myClass* myClass_object = new myClass( 33 );
int i = 0;
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
i = myClass_object->get();
cout << "Value is: " << i << endl;
return 0;
}
I tried to find a solution; I don't think it's the Code that's wrong, but the setting within eclipse. Can anyone help, I just started programming with MinGW and eclipse and it's frustrating that such an easy example fails.
Thank you very much,
Sebastian