Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming gpu hard drive hardware hdmi internet laptop malware memory monitor motherboard music network printer problem ram registry router server slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: C++ Linker error - "already defined in stdafx.obj"

Reply  
Thread Tools
andythepandy's Avatar
Computer Specs
Member with 278 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
28-Oct-2009, 08: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.
Malware Removal Specialist with 3,260 posts.
 
Join Date: Feb 2002
29-Oct-2009, 01: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
Computer Specs
Member with 278 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
29-Oct-2009, 07: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
Computer Specs
Member with 278 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
29-Oct-2009, 07: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
Computer Specs
Member with 278 posts.
 
Join Date: Jul 2006
Location: England
Experience: Intermediate to Advanced
31-Oct-2009, 11:04 AM #5
I fixed it now. I needed to put 'static' in front of each variable.

Thanks for all the help
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




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



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:07 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.