-1

I'm working on my first simple game with sdl and c++, and I have a couple of variables. First I have a gameRunning boolean, secondly I have a SDL_Surface called screen and I have a SDL_Rect called wall.

The compiler says that I have multiple definitions of all the variables mentioned before. When the definitions are in my .h file it gives the error, but when I cut and paste them into my cpp file it works fine.

I am sure that the definitions are not in the cpp file.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Not sure what you're asking. Define variables in the .cpp file. You already know this. So just do that? When you define them in a .h file and include that file in multiple source files, you naturally end up with multiple copies of the definitions... – Lightness Races in Orbit Jul 25 '15 at 23:09
  • 2
    I'm going to guess that you include your header file into more than one .cpp file. One way to address that problem is discussed in http://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files – Frank Boyne Jul 25 '15 at 23:10
  • Include guard is protecting you only from including the same file multiple times in the same compilation. If there are multiply source files including the same header you will still get multiple definitions. You can make your variables static or preferably remove them from header. Putting variables into the header is bad practice. – gandgandi Jul 25 '15 at 23:14
  • Are you sure that it’s the *compiler* complaining? Please show us the exact output you are receiving. I suspect that it is in fact the linker. – Jonas Schäfer Jul 25 '15 at 23:19
  • 2
    Declare your variables in the header file as `extern` and then define them in a source file. That way you are not defining it more than once, you are just declaring it a few times. – ChajusSaib Jul 25 '15 at 23:23
  • 2
    Please post your code, without it we can't help you – Mido Jul 25 '15 at 23:25

1 Answers1

1

Looks like multiple header file issue.

Solution is to refer variable e.g. gameRunning as extern in header file.

Similar issue and solution you can refer at: c++ multiple definitions of a variable

If not the case, please put starting lines where you are including header files here.

Community
  • 1
  • 1