# Solved: C++ Linker error - "already defined in stdafx.obj"



## andythepandy (Jul 2, 2006)

Hi all,
I have a project which is using Direct3D and DirectInput. Initially I had all the global variables in the main file, BE1.cpp. This worked fine.
To clean up the code a bit, I added a header file, gv.h, to hold all my variables. This header file is included in 'stdafx.h' - which is included in all .cpp files.

This way of doing it throws up the errors:

1>Building1Explore.obj : error LNK2005: "struct IDirect3D9 * d3d" ([email protected]@[email protected]@A) already defined in stdafx.obj
1>Building1Explore.obj : error LNK2005: "struct IDirect3DDevice9 * d3ddev" ([email protected]@[email protected]@A) already defined in stdafx.obj
1>Building1Explore.obj : error LNK2005: "struct ID3DXMesh * buildingMesh" ([email protected]@[email protected]@A) already defined in stdafx.obj
1>Building1Explore.obj : error LNK2005: "unsigned long numMaterials" ([email protected]@3KA) already defined in stdafx.obj
1>Building1Explore.obj : error LNK2005: "struct _D3DMATERIAL9 * material" ([email protected]@[email protected]@A) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "struct IDirectInput8W * din" ([email protected]@[email protected]@A) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "struct IDirectInputDevice8W * dinKeyboard" ([email protected]@[email protected]@A) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "struct IDirectInputDevice8W * dinMouse" ([email protected]@[email protected]@A) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "unsigned char * KeyState" ([email protected]@3PAEA) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "struct _DIMOUSESTATE MouseState" ([email protected]@[email protected]@A) already defined in stdafx.obj
1>C:\Users\Andy\Documents\Visual Studio 2008\Projects\DirectX Files\Building1Explore\Debug\Building1Explore.exe : fatal error LNK1169: one or more multiply defined symbols found

Each error is for each of the variables in gv.h
Any ideas on what could be causing the errors?

Thanks a lot,
Andy


----------



## IMM (Feb 1, 2002)

There are several things you can look at there to avoid the multiple inclusion of these .

You might want to use an ifndef block to enclose the contents of your header file
(see the b.h example in http://www.cplusplus.com/forum/beginner/7877/ )
or use *#pragma once* 
http://en.wikipedia.org/wiki/Pragma_once
to avoid it

The other thing you can look are the header settings for your VC -- Is it set to use pre-compiled headers? -- if so you could change the option
If your program is set up so that you can include this header for only one of the cpp files you can remove it from stdafx and include it for that cpp file only.


----------



## andythepandy (Jul 2, 2006)

Hi, thanks for the reply.
I have used:

#ifndef GV
#define GV
....variables here....
#endif

however it is still throwing the same errors as before. I did also try #pragma but the same happens.:-(
Thanks
Andy


----------



## andythepandy (Jul 2, 2006)

And also I forgot to say, it is set up for pre-compiled headers at the moment. I played around with that setting and where I was including the file but still those errors.

Andy


----------



## andythepandy (Jul 2, 2006)

I fixed it now. I needed to put 'static' in front of each variable.

Thanks for all the help


----------

