'Multiple definition of' error

Hello. I am having an issue in my code that I did not have yesterday. When I try to compile my code, I get about 20 multiple definition of '%s'', my variable and the same amount that says 'first defined here'. None of them have any line numbers and it is getting really annoying. I have included the first 10 lines of code for all my classes. if you need more, please let me know. I would really appreciate any help.

DOTA.cpp
1
2
3
4
5
6
7
8
9
10
#include <allegro.h>
#include "DOTA.h"
#include "SELECTBOX.h"
#include "HUD.h"

int install_timer();
bool TGridOn = false;
int mx, my, mb;
int pic1_x = -100;
int pic1_y = 0;


DOTA.h
1
2
3
4
5
6
7
8
9
10
#include <allegro.h>
#include <string>

#ifndef DOTA_H
#define DOTA_H

#define BLACK makecol(0,0,0)
#define WHITE makecol(255,255,255)
#define GREEN makecol(0,255,0)
#define PINK makecol(255,0,255) 


SELECTBOX.cpp
1
2
3
4
5
6
7
8
9
10
#include <allegro.h>
#include <string>

#include "SELECTBOX.h"

SELECTBOX::SELECTBOX(int xone,int yone,int xtwo,int ytwo)
{
     x1 = xone;
     x2 = xtwo;
     y1 = yone;


SELECTBOX.h
1
2
3
4
5
6
7
8
9
10
#include <allegro.h>
#include <string>

#ifndef SELECTBOX_H
#define SELECTBOX_H 

class SELECTBOX
{

public:


HUD.cpp
1
2
3
4
5
6
7
8
9
10
#include <allegro.h>
#include <string>

#include "HUD.h"


HUD::HUD(BITMAP *done)
{
    palette = done;
}


HUD.h
1
2
3
4
5
6
7
8
9
10
#include <allegro.h>
#include <string>

#ifndef HUD_H
#define HUD_H 

class HUD
{

public:


Thank you in advance.
We need to know which variable and where it's being repeatedly defined.
Would you like the game files? I can email them to you because honestly I have no idea where the error is coming from.
The error messages tell you which variable you are defining multiple times. This means that you are creating it in more than one place. Identify that variable, create it in only one place.
I am only creating them in DOTA.h and (for the most part) using them in DOTA.cpp
How many files include DOTA.h?
Post the full error messages and the files which they are referencing (the entire files if possible).
The error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  multiple definition of `gameover' 
  first defined here 
  multiple definition of `buildPhase' 
  first defined here 
  multiple definition of `buildPhaseBegin' 
  first defined here 
  multiple definition of `scores' 
  first defined here 
  multiple definition of `lives' 
  first defined here 
  multiple definition of `gold' 
  first defined here 
  multiple definition of `scrollx' 
  first defined here 
  multiple definition of `scrolly' 
  first defined here 
  multiple definition of `scrollz' 
  first defined here  


This was only about half of the errors, but you get the idea.

This is the entire DOTA.h file
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 <allegro.h>
#include <string>

#ifndef DOTA_H
#define DOTA_H

#define BLACK makecol(0,0,0)
#define WHITE makecol(255,255,255)
#define GREEN makecol(0,255,0)
#define PINK makecol(255,0,255)
#define RED makecol(255,0,0)
#define BLUE makecol(0,0,255)
#define WIDTH 640
#define HEIGHT 480


typedef struct MINION
{
    int player;
    int health, str, pdef, mdef, spd, attspd;
    int lane;
    bool alive;
    BITMAP *pic;
    int width, height;
    
}MINION;

typedef struct TOWER
{
    int player;
    int pos;
    BITMAP *pic;
    int width, height;
    
}TOWER;

typedef struct ATTACK
{
    int player;
    int str,attspd;
    bool magic;
    BITMAP *pic;
    bool canfire;
    
}ATTACK;

//vector <MINION> Troops;
bool gameover = false;
bool buildPhase = true;
bool buildPhaseBegin = true;
int scores[2];
int lives[2];
int gold[2];
int scrollx,scrolly;
int scrollz = 0;
int maxX, maxY;
double Mx, My;
double scale;

//These guys are temporary
bool inP1A = false;
bool inP1B = false;
bool inP1C = false;
bool inP2A = false;
bool inP2B = false;
bool inP2C = false;




BITMAP *backpanel;
BITMAP *background;
BITMAP *menuPanel1;
BITMAP *BPMB1;

BITMAP *MTowerB;
BITMAP *MTowerBS;

BITMAP *TGrid;
BITMAP *Pointer1;

BITMAP *buffer;
BITMAP *buffer2;

BITMAP *C2BM1;
BITMAP *BasicMinionB;
BITMAP *BasicMinionR;


void init();
void deinit();
void update();
void display();
void setXandY(double scale);



#endif 


This is the entire DOTA.cpp file
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <allegro.h>
#include "DOTA.h"
#include "SELECTBOX.h"
#include "HUD.h"

int install_timer();
bool TGridOn = false;
int mx, my, mb;
int pic1_x = -100;
int pic1_y = 0;

SELECTBOX* p1A = new SELECTBOX(12,415,38,430);
SELECTBOX* p1B = new SELECTBOX(70,415,85,430);
SELECTBOX* p1C = new SELECTBOX(70,455,85,478);
SELECTBOX* p2A = new SELECTBOX(415,12,430,38);
SELECTBOX* p2B = new SELECTBOX(415,70,430,85);
SELECTBOX* p2C = new SELECTBOX(455,70,478,85);

HUD* GUI = new HUD(backpanel);

int main()
{
	init();
	
	gold[0] = 1000;
	gold[1] = 1000;
    
    buffer = create_bitmap(653, 455);
    buffer2 = create_bitmap(800, 600);
	Pointer1 = load_bitmap("Images/MousePointer1.bmp", NULL);
    background = load_bitmap("Images/Concept6.bmp", NULL);
    MTowerB = load_bitmap("Images/Magic Tower B.bmp", NULL);
    MTowerBS = load_bitmap("Images/Magic Tower B S.bmp", NULL);
    TGrid = load_bitmap("Images/Concept6Grid.bmp", NULL);
    C2BM1 = load_bitmap("Images/ClickToBuildMinion1.bmp",NULL);
    BasicMinionB = load_bitmap("Images/Basic Minion B.bmp",NULL);
    BasicMinionR = load_bitmap("Images/Basic Minion R.bmp",NULL);
    menuPanel1 = load_bitmap("Images/DOTABeginMenu.bmp",NULL);
    backpanel = load_bitmap("Images/BackPanel2.bmp",NULL);
    BPMB1 = load_bitmap("Images/BuildPhaseMenuBar1.bmp",NULL);
    set_mouse_sprite(Pointer1);
    scale = 1.5;

    setXandY(2);
    
    blit(menuPanel1,screen,0,0,0,0,menuPanel1->w,menuPanel1->h);
    readkey();
	while (!key[KEY_ESC]) 
    {
        if(buildPhase) //Build Phase
        {
            update();
            display();
            rest(1);
        }
        else //Combat Phase
        {
            update();
            display();
            rest(1);
        }
	}

	deinit();
	return 0;
}
END_OF_MAIN()

void init()
{
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	// One of these MUST be disabled
	//res = set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}
    
	install_timer();
	install_keyboard();
	install_mouse();
}

void deinit()
{
	clear_keybuf();
}

void update()
{
     inP1A = false;
     inP1B = false;
     inP1C = false;
     inP2A = false;
     inP2B = false;
     inP2C = false;
     mx = mouse_x;
     my = mouse_y;
     mb = (mouse_b & 1);
     Mx = (mouse_x/scale + scrollx);
     My = (mouse_y/scale + scrolly);
     
     if(key[KEY_W])
     {
        if(scrolly>0)
           scrolly-=2;
        else
            scrolly = 0;
     }
     if(key[KEY_A])
     {
        if(scrollx>0)
           scrollx-=2;
        else
            scrollx = 0;
     }
     if(key[KEY_S])
     {
        if(scrolly<maxY)
           scrolly+= 2;
        else
            scrolly = maxY;
     }
     if(key[KEY_D])
     {
        if(scrollx<maxX)
           scrollx+= 2;
        else
            scrollx = maxX;
     }
     
     if(p1A->isInBox(Mx,My))
            inP1A = true;
     if(p1B->isInBox(Mx,My))
            inP1B = true;
     if(p1C->isInBox(Mx,My))
            inP1C = true;
            
         
     if(mouse_z > scrollz)
     {
        scale += 0.1;
        setXandY(scale);
        scrollz = mouse_z;
     }
     if(scale > 1)
     { 
        if(mouse_z < scrollz)
        {
           scale -= 0.1;
           setXandY(scale);
           scrollz = mouse_z;
        }
     }     
         
     if(key[KEY_1])
        setXandY(1);
     if(key[KEY_2])  
        setXandY(1.5);
     if(key[KEY_3])
        setXandY(2);
     if(key[KEY_4])
        setXandY(3);
     
     if(key[KEY_G])
         TGridOn = true;
     if(key[KEY_H])
         TGridOn = false;
}
    
}


(I took out void display() so that I can fit)
This is the void display()
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
void display()
{
    scare_mouse();
    acquire_screen();
    clear_bitmap(buffer);
    clear_bitmap(buffer2);

    
    stretch_blit(background, buffer, scrollx, scrolly, background->w, background->h, 0, 0, int(background->w*scale), int(background ->h*scale));
     
    if(inP1A)
    {
        blit(C2BM1, buffer,0,0,int(mx+10),int(my+35),C2BM1->w,C2BM1->h); 
        if(mb);
    }
    if(inP1B)
    {
        blit(C2BM1, buffer,0,0,int(mx+10),int(my+35),C2BM1->w,C2BM1->h); 
        if(mb);
    }
    if(inP1C)
    {
        blit(C2BM1, buffer,0,0,int(mx+10),int(my+35),C2BM1->w,C2BM1->h); 
        if(mb);
    }
     
    if(TGridOn == true)
    {
        masked_stretch_blit(TGrid, buffer, scrollx, scrolly, TGrid->w,TGrid ->h,0,0, int(TGrid->w*scale), int(TGrid ->h*scale));
    }
    
    if(mx+int(scale*scrollx > 470) && mx+ int(scale*scrollx) < 510)
    {
        //textprintf_ex(buffer,font,1,200,WHITE,0, "X condition met");
        if(my+int(scale*scrolly) > 310 && my+int(scale*scrolly) < 350)
        {
            //textprintf_ex(buffer,font,1,220,WHITE,0, "X and Y conditions met");
            masked_stretch_blit(MTowerBS, buffer, 0,0,MTowerBS->w,MTowerBS ->h, int(228 - scrollx * scale), int(148 - scrolly * scale), int(MTowerBS->w*scale), int(MTowerBS ->h*scale));
        }
    }
    
    stretch_sprite(buffer, MTowerB, int(230 - scrollx * scale), int(150 - scrolly * scale), int(MTowerB->w*scale), int(MTowerB->h*scale)); 
    draw_sprite(buffer, MTowerB, int(320 - scrollx * scale), int(210 - scrolly * scale));
    
    masked_blit(backpanel, buffer2, 0, 0, 0, 0, backpanel->w-1, backpanel->h-1);
     
    
    /*                    Show this for testing purposes
    */
    textprintf_ex(buffer,font,1,5,WHITE,-1, "X on Screen: %d",mouse_x);
    textprintf_ex(buffer,font,1,15,WHITE,-1, "Y on Screen: %d",mouse_y);
    textprintf_ex(buffer,font,1,30,WHITE,-1, "X on Background: %d",int(Mx));
    textprintf_ex(buffer,font,1,40,WHITE,-1, "Y on Background: %d",int(My));
    textprintf_ex(buffer,font,1,65,WHITE,-1, "X: %d",scrollx);
    textprintf_ex(buffer,font,1,75,WHITE,-1, "Y: %d",scrolly);
    textprintf_ex(buffer,font,1,100,WHITE,-1, "Scale: %f",scale);
    
    textprintf_ex(buffer2,font,10,470,BLACK,-1, "Player 1 gold: %d",gold[0]);
    textprintf_ex(buffer2,font,10,480,BLACK,-1, "Player 2 gold: %d",gold[1]);
    
    p1A->setUpBoxes1(background, BLACK);
    
    
    pic1_x += 7;
    
    if(pic1_x < 550)
        stretch_blit(BPMB1,buffer,0,0,BPMB1->w,BPMB1->h,pic1_x,200,BPMB1->w,BPMB1->h);
    
    
    masked_blit(buffer2, screen, 0, 0, 0, 0, buffer2->w, buffer2->h);
    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
    release_screen();
    
    show_mouse(screen);
    unscare_mouse();
}

void setXandY(double shcale)
{
    scale = shcale;
    if(shcale == 1)
    {
        maxX = 0;
        maxY = 38;
    }   
    else if(shcale == 1.5)
    {
        maxX = 58;
        maxY = 190;
    }
    else if(shcale == 2)
    {
        maxX = 168;
        maxY = 266;
    }
    else if(shcale == 3)
    {
        maxX = 277;
        maxY = 342;
    }
    else
    {
        maxX = 350;
        maxY = 450;
    }
    
    if(scrollx>maxX)
        scrollx = maxX;
    if(scrolly>maxY)
        scrolly = maxY;
    
    
}
Since all those are in DOTA.h, I'd guess that you're including DOTA.h in more than one cpp file, and thus you're trying to create the following variables more than once.

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
//vector <MINION> Troops;
bool gameover = false;
bool buildPhase = true;
bool buildPhaseBegin = true;
int scores[2];
int lives[2];
int gold[2];
int scrollx,scrolly;
int scrollz = 0;
int maxX, maxY;
double Mx, My;
double scale;

//These guys are temporary
bool inP1A = false;
bool inP1B = false;
bool inP1C = false;
bool inP2A = false;
bool inP2B = false;
bool inP2C = false;




BITMAP *backpanel;
BITMAP *background;
BITMAP *menuPanel1;
BITMAP *BPMB1;

BITMAP *MTowerB;
BITMAP *MTowerBS;

BITMAP *TGrid;
BITMAP *Pointer1;

BITMAP *buffer;
BITMAP *buffer2;

BITMAP *C2BM1;
BITMAP *BasicMinionB;
BITMAP *BasicMinionR;


Last edited on
That's what I though orignially, but I carefully combed through my code, and I only ever call DOTA.h in DOTA.cpp. You can check the #includes in the original post.
The problem comes at link time - might be worth checking to see that what you are linking is what you think you're linking, and that nothing unexpected is sneaking in there (leftover objects from old versions, or multiple links of the same object). If in doubt, shred the contents of the object directory and force a complete rebuild of the objects.
Last edited on
Alright, I'll try almost anything at this point. How does one go about shredding the object directory?
Depends on your IDE; on the assumption that it doesn't do a full rebuild of an object file if the code hasn't changed, somewhere it's got a directory full of object files (under *nix commonly named things like main.o and DOTA.o and what have you). If you delete them yourself, a rebuild of each one is forced.
Moshchops, you are a god among men. I deleted the .o files, recompiled, and it worked wonderfully. Thank you very much sir!

Now, if you don't mind, I was wondering how I might include DOTA.h into one of the other classes, then include that into DOTA.cpp without it causing that error again.
Last edited on
Replace the definitions in DOTA.h with extern declarations:

1
2
extern BITMAP *MTowerB;
extern BITMAP *MTowerBS;

Put the definitions into a .cpp file.

Defining variables in an .h file is bad practice.
Last edited on
Topic archived. No new replies allowed.