GLUT: Multiple Definitions/First Defined here Error

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:

Main.cpp/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "Timer.h"
#include "GLHandler.h"
#include <GL/glut.h>



void init(void){

  glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_POLYGON);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(h1->var1, 0.0, 0.0);
            glVertex3f(h1->var1, h1->var1, 0.0);
            glVertex3f(0.0, h1->var1, 0.0);
        glEnd();
        glFlush();

}



int main(int i,char ** j){


t1->init();
t1->updateTimer(init);


h1->display(i,j,init);



return (0);
}


Timer.cpp/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <GL/glut.h>
#include "Timer.h"
#include <iostream>

using namespace 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(const char * 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?
It means that variable h1 shall be defined only once if it has external linkage.
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?

Here are the headers:

GLHandler.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef GLHANDLER_H_H
#define GLHANDLER_H_H



class GLHandler{




public:
  GLHandler(void);
  void display(int,char**,void(*)());
  float var1;


};

GLHandler * h1 = new GLHandler();


#endif 


Timer.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef TIMER_H
#define TIMER_H




  class Timer
  {
  public:


  Timer(int);
  void updateTimer(void(*f)(void));
  void init(void);
  void reset(int);
  void term(void);
  void onTick(void(*f)());
  void prnt(const char*);

  private:

  long timeSyncAdjust;
  int updates;
  bool resetted;
  bool run;
  int counter;

  };

  Timer * t1 = new Timer(10000);

#endif 


And if needed, the cpp's

GLHandler.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <GL/glut.h>
#include "GLHandler.h"

using namespace std;

GLHandler::GLHandler(void){

  this->var1 = 0.5f;

}


void GLHandler::display(int i,char**j,void(*call)()){


            glutInit(&i,j);
            glutInitDisplayMode(GLUT_SINGLE);
            glutInitWindowSize(300, 300);
            glutInitWindowPosition(100, 100);
            glutCreateWindow("Hello world :D");
            glutDisplayFunc(*call);
            glutMainLoop();

}



Timer.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <GL/glut.h>
#include "Timer.h"
#include <iostream>
#include "GLHandler.h"







using namespace 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(const char * 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.5f;
         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;
}


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?
Last edited 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

GLHandler.cpp

1
2
3
4
5
6
7
8
#include <GL/glut.h>
#include "GLHandler.h"

using namespace 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

GLHandler.cpp





12345678
#include <GL/glut.h>
#include "GLHandler.h"

using namespace std;

GLHandler * h1 = new GLHandler();

// and so on



Thank you vlad! This cleared things up for me.
Last edited on
Topic archived. No new replies allowed.