Global variable doesn't work

I tried to create a global boolean to use in two different source files.

This is the header file, included in both source files:
1
2
3
4
5
6
#ifndef GLOBALS_H
#define GLOBALS_H

extern bool quitgame;

#endif 


I initialized the boolean in the main source file:
bool quitgame = false;

It works well in that file, but when I used it another source file like this:
quitgame = true;

g++ gives an error:
1
2
/tmp/ccLZQa5J.o: In function `Player::MovePlayer()':
player.cpp:(.text+0x16b): undefined reference to `quitgame'
Are you linking both files into the final binary?
Yes :|
Then I give up.
are you including your header file in another source file.

Note: Include the header file in both source files.
use g++ -g -o main file1.cpp file2.cpp.

it will work.
Topic archived. No new replies allowed.