Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Software Development
Tag Cloud
access audio black screen blue screen boot bsod connection crash dell desktop driver drivers dvd email error excel excel 2003 firefox hard drive hardware hdmi hijackthis internet itunes keyboard laptop malware monitor motherboard network networking outlook problem recovery router screen slow sound spyware trojan upgrade video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: C++ Linker error - "already defined in stdafx.obj"

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

 
Thread Tools
andythepandy's Avatar
Member with 66 posts.
 
Join Date: Jul 2006
28-Oct-2009, 07:51 AM #1
Solved: C++ Linker error - "already defined in stdafx.obj"
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" (?d3d@@3PAUIDirect3D9@@A) already defined in stdafx.obj
1>Building1Explore.obj : error LNK2005: "struct IDirect3DDevice9 * d3ddev" (?d3ddev@@3PAUIDirect3DDevice9@@A) already defined in stdafx.obj
1>Building1Explore.obj : error LNK2005: "struct ID3DXMesh * buildingMesh" (?buildingMesh@@3PAUID3DXMesh@@A) already defined in stdafx.obj
1>Building1Explore.obj : error LNK2005: "unsigned long numMaterials" (?numMaterials@@3KA) already defined in stdafx.obj
1>Building1Explore.obj : error LNK2005: "struct _D3DMATERIAL9 * material" (?material@@3PAU_D3DMATERIAL9@@A) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "struct IDirectInput8W * din" (?din@@3PAUIDirectInput8W@@A) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "struct IDirectInputDevice8W * dinKeyboard" (?dinKeyboard@@3PAUIDirectInputDevice8W@@A) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "struct IDirectInputDevice8W * dinMouse" (?dinMouse@@3PAUIDirectInputDevice8W@@A) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "unsigned char * KeyState" (?KeyState@@3PAEA) already defined in stdafx.obj
1>DirectInput.obj : error LNK2005: "struct _DIMOUSESTATE MouseState" (?MouseState@@3U_DIMOUSESTATE@@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's Avatar
IMM IMM is offline IMM is authorized to help remove malware.
Distinguished Member with 3,232 posts.
 
Join Date: Feb 2002
29-Oct-2009, 12:54 AM #2
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's Avatar
Member with 66 posts.
 
Join Date: Jul 2006
29-Oct-2009, 06:32 AM #3
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's Avatar
Member with 66 posts.
 
Join Date: Jul 2006
29-Oct-2009, 06:34 AM #4
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's Avatar
Member with 66 posts.
 
Join Date: Jul 2006
31-Oct-2009, 10:04 AM #5
I fixed it now. I needed to put 'static' in front of each variable.

Thanks for all the help
Reply Bookmark and Share

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 07:42 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.