-2

Including Settings.h into Main.cpp, on compiling, the following errors appear:

class WindowData * windowData" (?windowData@@3PEAVWindowData@@EA) already defined in Main.obj Settings.obj 1

class Time * timeControl" (?timeControl@@3PEAVTime@@EA) already defined in Main.obj Settings.obj 1

class Camera * cam1" (?cam1@@3PEAVCamera@@EA) already defined in Main.obj
Settings.obj 1

class InputManager * inputManager" (?inputManager@@3PEAVInputManager@@EA) already defined in Main.obj
Settings.obj 1

class ShaderManager * shaderManager" (?shaderManager@@3PEAVShaderManager@@EA) already defined in Main.obj
Settings.obj 1

one or more multiply defined symbols found MORPH_Client.exe 1

Settings.h

#pragma once
#include "WindowData.h"
#include "Time.h"
#include "Camera.h"
#include "ShaderManager.h"
#include "InputManager.h"

WindowData* windowData;
Time* timeControl;
Camera* cam1;
InputManager* inputManager;
ShaderManager* shaderManager;

Settings.cpp

#include "Settings.h"

Main.cpp

#include "Settings.h"
int main()
{
    return 0;
}

Can someone explain to me what I may be doing wrong?

Settings.h is meant to be a sort of header for the main file, where some OpenGL functions are executed for startup. This is the best way I could set this up that I could think of, since some of OpenGL's event informing functions require callback functions that can't be class methods. I would have encapsulated everything in a single SetupClass if it wheren't for that, and I couldn't really figure any other code structure.

I'm watching over this help request, so if more info is needed I'll add it immediately.

Community
  • 1
  • 1
Mithrandir
  • 33
  • 6
  • "Can someone explain to me what I may be doing wrong?" you not suppose to include .cpp file. But please read https://stackoverflow.com/help/how-to-ask (about links to source code in your question) – Slava Mar 03 '20 at 19:28
  • @Slava thank you, but where exactly am I including a .cpp file? – Mithrandir Mar 03 '20 at 19:32
  • 1
    "Including Settings.cpp into Main.cpp on execution" what that suppose to mean? Anyway edit youyr question to contain [mcve] and follow the instructions on that link if you want to get proper help – Slava Mar 03 '20 at 19:33
  • @Salva my bad. I'll edit the question with the corrections. – Mithrandir Mar 03 '20 at 19:35
  • Hope this fits the standards, else I'll continue editing. – Mithrandir Mar 03 '20 at 20:01
  • @Salva reopen ? – Mithrandir Mar 03 '20 at 20:10
  • 1
    Copy the variables currently in your settings.h into the settings.cpp file, then, in the settings.h file, mark them all with `extern` – ChrisMM Mar 03 '20 at 20:24
  • @ChrisMM thank you, this fixes it, but I'm not quite sure why. – Mithrandir Mar 03 '20 at 20:44
  • Save that as an answer @ChrisMM... but basically because you want to treat them as globals since you didn't put them inside a namespace or a class and singly instantiate them; they get instantiated in each of your compilation modules – UpAndAdam Mar 03 '20 at 22:07
  • They where inside a namespace, it's just that I got paranoid about it. – Mithrandir Mar 03 '20 at 22:40
  • @UpAndAdam, couldn't do as answer since question was already closed. – ChrisMM Mar 04 '20 at 00:05
  • 1
    Duplucate of [How do I use extern to share variables between source files?](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files/1433387) – 273K Mar 04 '20 at 01:24
  • @ChrisMM I was saying that because I helped reopen it :-) – UpAndAdam Mar 04 '20 at 16:37
  • @UpAndAdam, ah, when I posted that comment I was on my phone, so didn't notice until this morning. – ChrisMM Mar 04 '20 at 17:30
  • @ChrisMM all good, glad to +1 your answer! – UpAndAdam Mar 04 '20 at 20:56

1 Answers1

2

Now that the question has been reopened, I'll post this as an answer instead of a comment.

Basically, the issue is that, when compiling, each time the compiler sees WindowData* windowData; (for example), it will create a new variable and know to allocate space for windowData. Later, the linker is trying to put this all together, and realizes there's more than one windowData variable, so you get the "already defined" error you are getting.

To solve it, you only want to define the variables once, most likely in your "Settings.cpp" file. Then, for the header file, you would mark each declaration with extern, so that the compiler will know that variable exists somewhere, but won't try to allocate anything to it.

So, something like this:

Settings.h

#pragma once
#include "WindowData.h"
#include "Time.h"
#include "Camera.h"
#include "ShaderManager.h"
#include "InputManager.h"

extern WindowData* windowData;
extern Time* timeControl;
extern Camera* cam1;
extern InputManager* inputManager;
extern ShaderManager* shaderManager;

Settings.cpp

#include "Settings.h"

WindowData* windowData;
Time* timeControl;
Camera* cam1;
InputManager* inputManager;
ShaderManager* shaderManager;
ChrisMM
  • 8,448
  • 13
  • 29
  • 48