Jump to content
  • Advertisement
Sign in to follow this  
TutenStain

"Already defined in .obj" - C++

This topic is 3696 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

Hi! I'm new to these forums and I need some help. My current configuration looks like this: Game.h <--In this I keep the global variables. I declare them using "extern". In this I also include Main.h Main.cpp <--On the top of this file I have "#define MAIN_CPP" and then I include Main.h and Game.h. Main.h <--In this I have "#ifndef MAIN_H #define MAIN_H...someRandomFunctios #endif". Active.cpp <--In this I include Game.h Init.cpp <--In this I include Game.h The problem is that im getting an error that says: 1>Init.obj : error LNK2005: "int const SCREEN_WIDTH" (?SCREEN_WIDTH@@3HB) already defined in Active.obj 1>Init.obj : error LNK2005: "int const SCREEN_HEIGHT" (?SCREEN_HEIGHT@@3HB) already defined in Active.obj 1>Init.obj : error LNK2005: "int const SCREEN_BPP" (?SCREEN_BPP@@3HB) already defined in Active.obj Anyone know how to fix this problem? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by TutenStain
Game.h <--In this I keep the global variables. I declare them using "extern". In this I also include Main.h

Did you put inclusion guards in Game.h as well? #ifndef GAME_H, #define GAME_H, #endif?

Quote:
Main.cpp <--On the top of this file I have "#define MAIN_CPP" (...)

This is unnecessary. Inclusion guards are there to prevent things from being defined more than once. However, .cpp files aren't normally included in other files, so there's no need for inclusion guards there.

EDIT: Where exactly did you put the definition of SCREEN_WIDTH, SCREEN_HEIGHT and SCREEN_BPP?

Share this post


Link to post
Share on other sites
I defined SCREEN_WIDTH, SCREEN_HEIGHT and SCREEN_BPP in Game.h like this "extern const int SCREEN_WIDTH = 640".

And if I dont define MAIN_CPP I get errors x2.

I also tried to put inclusion guards in the Game.h but I got the same error.

Share this post


Link to post
Share on other sites
Your problem is that you're declaring global variables in a header file. That's a nono.

All an #include statement does is basically paste the header file into your source code. So if you're declaring a global variable in the header, basically the compiler is just going to see the same variable declared in multiple files.

What you need to do is declare the variable in your source file, and then declare it with "extern" in the header file to tell the compiler that the variables in question are in a different translation unit (object file)

For example:

Game.h:

extern int SCREEN_WIDTH;
extern int SCREEN_HEIGHT;

Game.cpp:

int SCREEN_WIDTH = 800;
int SCREEN_HEIGHT = 600;

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!