I am creating a game in ray lib and have a player header file with a struct of player to keep things organised. I get this error saying that there is multiple definition of the struct 'player'.
Error:
/usr/bin/ld: main.o:(.bss+0x0): multiple definition of `player'; player.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit status
player.cpp:
#include <raylib.h>
#include "player.h"
void InitPlayer() {
player.x = 0;
player.y = 0;
player.tint = (Color)RAYWHITE;
player.Frame = 0;
player.FrameWidth = 16;
player.MaxFrames = 3;
player.animation_number = 0;
player.timer = 0.0f;
player.flip = 1;
//Load Texture2D
player.playerTexture = LoadTexture("Sprites/PlayerAll.png");
}
void UpdatePlayer() {
player.timer += GetFrameTime();
if(player.timer >= 0.2f){
player.timer = 0.0f;
player.Frame += 1;
}
if(IsKeyDown(KEY_S)) {
player.animation_number = 16;
player.y += 1;
}else if(IsKeyUp(KEY_S)){
player.animation_number = 0;
}
if(IsKeyDown(KEY_W)){
player.animation_number = 32;
player.y -= 1;
}
if(IsKeyDown(KEY_A)){
player.animation_number = 48;
player.x -= 1;'''
}
if(IsKeyDown(KEY_D)){
player.animation_number = 48;
player.x += 1;
player.flip = -1;
} else {
player.flip = 1;
}
//Clamp frame value
player.Frame = player.Frame % player.MaxFrames;
}
void DrawPlayer() {
DrawTexturePro(player.playerTexture,
Rectangle{player.FrameWidth*player.Frame,(float)player.animation_number,(float)player.flip*16,16},
Rectangle{(float)player.x,(float)player.y,16,16},
Vector2{0,0},0,player.tint);
}
void UnloadPlayer() {
UnloadTexture(player.playerTexture);
}
Player.cpp:
#ifndef PLAYER_H
#define PLAYER_H
typedef struct Player{
int x, y;
Color tint;
int Frame;
int FrameWidth;
int MaxFrames;
int animation_number;
float timer;
int flip;
Texture2D playerTexture;
}Player;
Player player;
void UnloadPlayer();
void InitPlayer();
void UpdatePlayer();
void DrawPlayer();
#endif
Main: The main.cpp' file basically calls the functions from the 'player.h' file. And I have included 'player.h' in 'main.cpp'
Just for some extra information if needed: I have tried using 'extern' on 'player' but it requires me to compile the cpp files in a strange order. After compiling and linking both object 'player.o' and 'main.o', I change 'player.cpp' to have 'extern' on struct global variable 'player' and compile 'player.cpp' only without compiling 'main.cpp' and link both objects files with the 'main.o' being the old one.