I’m using the code here and I want all the functions which call SDL functions to be in a different header file instead of the main .cpp file. So I created a new file called Methods.h.
Here is what the files look like:
main.cpp:
#include "Methods.h"
...
Methods.h:
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
....
And I removed all the functions from main and put them in Methods.h
When I try to build the project, it says “multiple definition error”.
/media/Data1/Projects/OpenGL/Game2/Methods.h|29|multiple definition of `event'|
obj/Debug/main.o:/media/Data1/Projects/OpenGL/Game2/Methods.h|29|first defined here|
obj/Debug/src/Machine.o||In function `drawFrame()':|
/media/Data1/Projects/OpenGL/Game2/Methods.h|29|multiple definition of `drawFrame()'|
obj/Debug/main.o:/media/Data1/Projects/OpenGL/Game2/Methods.h|29|first defined here|
obj/Debug/src/Machine.o||In function `init_GL()':|
/media/Data1/Projects/OpenGL/Game2/Methods.h|73|multiple definition of `init_GL()'|
obj/Debug/main.o:/media/Data1/Projects/OpenGL/Game2/Methods.h|73|first defined here|
obj/Debug/src/Machine.o||In function `init()':|
/media/Data1/Projects/OpenGL/Game2/Methods.h|97|multiple definition of `init()'|
obj/Debug/main.o:/media/Data1/Projects/OpenGL/Game2/Methods.h|97|first defined here|
obj/Debug/src/Machine.o||In function `clean_up()':|
/media/Data1/Projects/OpenGL/Game2/Methods.h|123|multiple definition of `clean_up()'|
obj/Debug/main.o:/media/Data1/Projects/OpenGL/Game2/Methods.h|123|first defined here|
It looks like you have some function definitions in your header file, this means that you’ll get a separate definition of each function from each source file that includes the header. They need to be declared
inlinein order to allow them to be defined in every translation unit that includes the header:Alternatively, you could just declare them in the header, and move the definitions to a source file:
There’s also a global variable. If you want to keep that, you’ll have to move the definition into a source file, and declare it
externin the header file. Alternatively, you could replace it with a function that accesses a static variable:This has the advantage, and the disadvantage, that it’s initialised the first time the function is called.