Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

global int already defined

This topic is 4337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive made an ugly solution for managing the settings in my game, I keep them as global variables in a file: Defines.h
#pragma once

int RESX = 1024;
int RESY = 768;
float CAMERADISTANCE = 100.0f;

The problem is during compile I get several of these errors:
error LNK2005: "int RESX" (?RESX@@3HA) already defined in BallManager.obj

It works if I instead decleare them consts or if I define them, but the reason Im doing this is because I want to change them runtime. Any ideas on whats wrong?

Share this post


Link to post
Share on other sites
Advertisement
That is because you are probably including that header from different .cpp files.

You need to define your globals in one .cpp file and for every other .cpp file that needs them you need to prefix the int and float keyword by an extern.

Edit:
The linker is actually seeing multiple definitions of the variables; one from each object file that gets created by the compiler.




// File1.cpp
int RESX = 1024;
int RESY = 768;
float CAMERADISTANCE = 100.0f;


// File2.cpp
extern int RESX;
extern int RESY;
extern float CAMERADISTANCE;



You can put this in a header file if you wish and include it for every .cpp file that uses these variables

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!