1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by andythepandy, Oct 28, 2009.

Thread Status:
Not open for further replies.
Advertisement
  1. andythepandy

    andythepandy Thread Starter

    Joined:
    Jul 2, 2006
    Messages:
    367
    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
     
  2. IMM

    IMM Malware Specialist

    Joined:
    Feb 1, 2002
    Messages:
    3,259
    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.
     
  3. andythepandy

    andythepandy Thread Starter

    Joined:
    Jul 2, 2006
    Messages:
    367
    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
     
  4. andythepandy

    andythepandy Thread Starter

    Joined:
    Jul 2, 2006
    Messages:
    367
    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
     
  5. andythepandy

    andythepandy Thread Starter

    Joined:
    Jul 2, 2006
    Messages:
    367
    I fixed it now. I needed to put 'static' in front of each variable.

    Thanks for all the help
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/872351

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice