fatal error LNK1169: one or more multiply defined symbols found in game programming
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.
You can't put variable definitions in header files, as these will then be a part of all source file you include the header into.
The #pragma once
is just to protect against multiple inclusions in the same source file, not against multiple inclusions in multiple source files.
You could declare the variables as extern
in the header file, and then define them in a single source file. Or you could declare the variables as const
in the header file and then the compiler and linker will manage it.