Linker errors probably external variable problem

Started by
4 comments, last by DevFred 14 years, 6 months ago
In the process of going from GLUT to SDL for a little OpenGL program. So far I'm working on initialising, and everything was working fine in a single main.cpp file, but when I started breaking up the program into separate header and source files I'm running into the problems I had when i tried to modularize my last project: how to handle global variables. My code is pretty small so I can post all of it here except 2 files which aren't involved in the problem.

///////////////main.cpp

#include <windows.h>
#include <iostream>

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

#include "system.h"
#include "game.h"

using namespace std;

int main(int argc, char **argv)
{
	cout << "Initializing...\n\n";
	if( init_SDL() )	{
		cout << "...SDL Initialized\n";
	}
	if( init_GL() )	{
		cout << "...OpenGL Initialized\n";
	}
	
	gameRunning = true;

	mainLoop();

	atexit(SDL_Quit);

	return 0;
}




//////////////game.h
#ifndef GAME_H
#define GAME_H

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

SDL_Event myEvent;

bool gameRunning;

void mainLoop();

#endif


///////////////game.cpp
#include "game.h"

void mainLoop()	
{
	while (gameRunning)	{
		if (SDL_PollEvent(&myEvent))	{
			if (myEvent.type == SDL_QUIT)	{
				gameRunning = false;
			}
		} 

		if (myEvent.type == SDL_KEYDOWN)	{
			SDLKey keyPressed = myEvent.key.keysym.sym;

			switch (keyPressed)	{
				case SDLK_ESCAPE:	
					gameRunning = false;
					break;
			}
		}

		glClear( GL_COLOR_BUFFER_BIT );

		SDL_GL_SwapBuffers();
	}
}

In this current state I get the following errors when trying to build with MSVC 2008 Express:

Linking...
game.obj : error LNK2005: "bool gameRunning" (?gameRunning@@3_NA) already defined in main.obj
game.obj : error LNK2005: "union SDL_Event myEvent" (?myEvent@@3TSDL_Event@@A) already defined in main.obj
C:\Documents and Settings\S\Desktop\P\SDLGL00\SDLGL00\Debug\SDLGL00.exe : fatal error LNK1169: one or more multiply defined symbols found
I tried moving the definition gameRunning = true; into the game.cpp file but that didn't work and gave me a different set of errors:

c:\documents and settings\s\desktop\p\sdlgl00\sdlgl00\game.cpp(3) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\s\desktop\p\sdlgl00\sdlgl00\game.cpp(3) : error C2371: 'gameRunning' : redefinition; different basic types
        c:\documents and settings\s\desktop\p\sdlgl00\sdlgl00\game.h(9) : see declaration of 'gameRunning'
Advertisement
You should think about better solutions than to use a global variable.
That being said, the following will work:

/// game.h:
extern bool gameRunning;

/// game.cpp:
bool gameRunning = true;

The problem with your first solution was that the symbol/variable "gameRunning" was multiply defined because it has been put into the header.
The problem with your second solution was that the variable could not be seen "from the outside world", because its declaration was missing, only its definition was given in the .cpp file.

-tiv
adding 'extern' prefix to the declaration in game.h doesn't seem to work either, i get unresolved external variable errors.
Did you include the corresponding header file in your .cpp?

The following surely works:


Test.h:

#pragma once

extern bool myTestVariable;



Test.cpp:

#include "Test.h"

bool myTestVariable = true;


Accessing "myTestVariable" is now just a matter of including "Test.h". However, I want to stress again that this is not good practice.

-tiv
Thanks.

From you suggestion I see I should have used

bool gameRunning = true;

instead of

gameRunning = true;

the type was needed then? for some reason that seems odd to me.

the second bit of errors were coming from SDL_Event myEvent because apparently declaring that in the header file was also defining it, so I moved it into the CPP file since main.cpp wont' be needing myEvent. the only problem is I may write an input cpp file. I don't know if I am breaking my program up correctly but I will see.
read me

This topic is closed to new replies.

Advertisement