MoonBun
MoonBun

Reputation: 4402

I have no idea what is this(C++ linker error)

getting this

1>main_display.obj : error LNK2005: "struct ALLEGRO_DISPLAY * main_display" (?main_display@@3PAUALLEGRO_DISPLAY@@A) already defined in event_queue.obj
1>main.obj : error LNK2005: "struct ALLEGRO_DISPLAY * main_display" (?main_display@@3PAUALLEGRO_DISPLAY@@A) already defined in event_queue.obj
1>main.obj : error LNK2005: "struct ALLEGRO_TIMER * timer" (?timer@@3PAUALLEGRO_TIMER@@A) already defined in event_queue.obj
1>main.obj : error LNK2005: "struct ALLEGRO_EVENT_QUEUE * event_queue" (?event_queue@@3PAUALLEGRO_EVENT_QUEUE@@A) already defined in event_queue.obj
1>main_timer.obj : error LNK2005: "struct ALLEGRO_TIMER * timer" (?timer@@3PAUALLEGRO_TIMER@@A) already defined in event_queue.obj

Any Idea what can cause this?

EDIT:

main_display.h:

#pragma once

#include <allegro5/allegro.h>
#include <stdio.h>

#define SCREEN_W 640
#define SCREEN_H 480

extern ALLEGRO_DISPLAY *main_display = NULL;

void display_init();
void destroy_display();

event_queue.h

#pragma once

#include <stdio.h>
#include <allegro5/allegro.h>
#include "main_timer.h"
#include "main_display.h"

extern ALLEGRO_EVENT_QUEUE *event_queue = NULL;

void event_queue_init();
void event_queue_destroy();

Upvotes: 0

Views: 122

Answers (3)

Dennis
Dennis

Reputation: 14477

It looks like you're defining the same struct in different files.

Without actually seeing the files, that's about as far as I get...

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308130

I'm guessing you put function implementations into a header (.h) file without using an inline declaration.

As the header file is included in multiple sources, the body of the function gets compiled multiple times. The linker is complaining about seeing the function more than once.

Upvotes: 0

Billy ONeal
Billy ONeal

Reputation: 106530

Looks like those structures are defined in a header file. Then they're #included into multiple translation units. You need to make it such that there's only one definition of the particular item.

Given that these are global variables, the way you generally do that is declaring them by marking them extern in the header, and then defining them in some translation unit.

Upvotes: 3

Related Questions