Declaring variable for use across multiple files.

Disclaimer: Noob question

I've decided to dive right into playing with code, no real prior coding experience. I'm using SFML to handle all the low-level stuff, and i've been following the tutorials. I've managed to display a window but now i'm trying to modify the code to be used across different files so I can organize the code. I've taken the tutorial examples, and tried to split them into several files:


window.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "game.h"
#include <iostream>

using namespace std;

int main()
{
	// Create the main window
	sf[::Window App(sf::VideoMode(800, 600, 32), "Game");

	// Start main loop
	bool Running = true;
	while (Running)
    	{
		while (App.IsOpened())
		{
			InputGet();
		}
	}

return EXIT_SUCCESS;
}


input.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "game.h"
#include <iostream>

using namespace std;

void InputGet(void)
{
	sf::Event Event;
	sf::Window App;
	while (App.GetEvent(Event))
	{
		// Window closed
		if (Event.Type == sf::Event::Closed)
			App.Close();
			Running = false;
		// Escape key pressed
		if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
			App.Close();
			Running = false;
	}
}


game.h:
1
2
3
4
#include <SFML/Window.hpp>

//Define function prototypes
void InputGet();


Problem is with the variable Running, when it comes to compiling it with GCC it gives me this error:

1
2
3
/tmp/cc8f8e7B.o: In function `InputGet()':
input.cpp:(.text+0x31): undefined reference to `Running'
input.cpp:(.text+0x58): undefined reference to `Running' 


I'm unsure how to properly declare the variable so both window.cpp and input.cpp will interact with it. I've tried googling and using the search but i'm not quite sure how to word my question.
Last edited on
stick it in a header file extern int variable , declare it in some cpp file int variable = 0; and include that header wherever you need the variable. xD BTW, its not a trivial matter to be doing this...
Thanks. I was stuck on that one for a bit. Compiles at least, now to make it work haha
BTW, doing this is not something you want to be doing. It should be avoided as much as possible.
Topic archived. No new replies allowed.