The two int
variables are defined in the header file. This means that every source file which includes the header will contain their definition (header inclusion is purely textual). The of course leads to multiple definition errors.
You have several options to fix this.
- Make the variables
static
(static int WIDTH = 1024;
). They will still exist in each source file, but their definitions will not be visible outside of the source file. - Turn their definitions into declarations by using
extern
(extern int WIDTH;
) and put the definition into one source file:int WIDTH = 1024;
. - Probably the best option: make the variables
const
(const int WIDTH = 1024;
). This makes themstatic
implicitly, and also allows them to be used as compile-time constants, allowing the compiler to use their value directly instead of issuing code to read it from the variable etc.