I am a novice programmer experimenting with GLUT and have come across a problem that I cannot resolve. Here below is the classes that are involved in this error:
#include <GL/glut.h>
#include "Timer.h"
#include <iostream>
usingnamespace std;
Timer::Timer(int par1){
this->counter = par1;
this->timeSyncAdjust = 1.0L;
this->run = false;
this->resetted = false;
this->updates = 0;
}
void Timer::init(void){
std::cout<<"INIT"<<std::endl;
this->run = true;
}
void Timer::onTick(void (*f)()){
(*f)();
std::cout<<"TICK"<<std::endl;
}
void Timer::prnt(constchar * f){
std::cout<<f<<std::endl;
}
void Timer::updateTimer(void(*argf)(void)){
int var1 = 0;
this->counter /= timeSyncAdjust;
while(this->run){
var1++;
if(var1 >= this->counter){
prnt("++");
this->updates++;
h1->var1 += 0.5;
this->onTick(argf);
var1 = 0;
}
if(this->updates > 10){
this->term();
std::cout<<"TERM"<<std::endl;
}
}
}
void Timer::reset(int par1){
int var1 = 0;
if(this->run){
this->run = false;
this->resetted = true;
}
while(!this->run){
var1++;
if(var1 >= par1){
this->run = true;
break;
}
}
}
void Timer::term(){
this->run = false;
}
And lastly, the error log:
[output]16:40:41 **** Incremental Build of configuration Debug for project GLUT ****
Info: Internal Builder is used for build
g++ -o GLUT.exe Timer.o Main.o GLHandler.o -lopengl32 -lglut32 -lglu32
Main.o: In function `glutInit_ATEXIT_HACK':
c:/FREEGLUT/freeglut/include/GL/freeglut_std.h:606: multiple definition of `h1'
Timer.o:c:/FREEGLUT/freeglut/include/GL/freeglut_std.h:606: first defined here
Main.o: In function `glutInit_ATEXIT_HACK':
c:/FREEGLUT/freeglut/include/GL/freeglut_std.h:606: multiple definition of `t1'
Timer.o:c:/FREEGLUT/freeglut/include/GL/freeglut_std.h:606: first defined here
GLHandler.o: In function `glutInit_ATEXIT_HACK':
c:/FREEGLUT/freeglut/include/GL/freeglut_std.h:606: multiple definition of `h1'
Timer.o:c:/FREEGLUT/freeglut/include/GL/freeglut_std.h:606: first defined here
collect2: ld returned 1 exit status
16:40:42 Build Finished (took 712ms)
[/output]
The two variables in the error log (t1 and h1)were both declared in their respective headers,Timer.h(t1) and GLHandler.h(h1). I would think that other classes should be able to access those variables, if that is the problem that is being created by my code. I have read elsewhere about the same error that I get, but I still cannot resolve this issue.
If any of you who answer require any other information,I'll be glad to post it.
The message text is clear enough. Variable h1 is defined several times. It seems that you placed the definition in a header file and included that header in several modules.
The message text is clear enough. Variable h1 is defined several times. It seems that you placed the definition in a header file and included that header in several modules.
So you are saying that you can only include the header that h1 and t1 are located in once?
From what I can see, It looks like both of those variables are defined only once in the header, but obviously I am wrong because I get an error, could you specifically tell me what I need to change?
They will be defined once for each source file they are included in. In total that becomes many times. A solution is to put an extern declaration in the header extern GLHandler * h1;
and but the definition in a source file. GLHandler * h1 = new GLHandler();
They will be defined once for each source file they are included in. In total that becomes many times. A solution is to put an extern declaration in the header
extern GLHandler * h1;
and but the definition in a source file.
GLHandler * h1 = new GLHandler();
Just a thought, does it matter if I declare h1 and t1 in a class or can it be done in the global scope?
Also, would you include the header where h1 is declared, or the .cpp file that corresponds to the header?
You are wrong saying that variable h1 is defined only once in header GLHandler.h. The compiler deals with translation units. You have at least two translation units: one that is formed from Timer.cpp and its including headers and other that is formed from GLHandler.cpp and its including headers. As the both translation units have source lines from GLHandler.h then the both define variable h1 that has external linkage. So the linker sees that there are two definitions of external variable h1 one of which is in the first translation unit and other in the second translation unit. And it does not know which definition to include into the result executable module.
You should place in the header only the declaration of h1 if it need to be visible in several translation ubits that will include the header. for example
GLHandler.h
extern GLHandler * h1;
and define the variable only in one translation unit, for example, in
GLHandler.cpp
1 2 3 4 5 6 7 8
#include <GL/glut.h>
#include "GLHandler.h"
usingnamespace std;
GLHandler * h1 = new GLHandler();
// and so on
You are wrong saying that variable h1 is defined only once in header GLHandler.h. The compiler deals with translation units. You have at least two translation units: one that is formed from Timer.cpp and its including headers and other that is formed from GLHandler.cpp and its including headers. As the both translation units have source lines from GLHandler.h then the both define variable h1 that has external linkage. So the linker sees that there are two definitions of external variable h1 one of which is in the first translation unit and other in the second translation unit. And it does not know which definition to include into the result executable module.
You should place in the header only the declaration of h1 if it need to be visible in several translation ubits that will include the header. for example
GLHandler.h
extern GLHandler * h1;
and define the variable only in one translation unit, for example, in