multiple defs

This is a discussion on multiple defs within the C++ Programming forums, part of the General Programming Boards category; More dll trouble -- when I include this header in multiple source files I get LINK2005 errors all are along ...

  1. #1
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

    multiple defs

    More dll trouble -- when I include this header in multiple source files I get LINK2005 errors

    all are along these lines
    error LNK2005: "struct IDirect3D9 * g_pD3D" (?g_pD3D@@3PAUIDirect3D9@@A) already defined in Audio.obj
    Code:
    #ifndef REAL_EARTH_ENGINE_H
    #define REAL_EARTH_ENGINE_H
    
    #include <d3d9.h>
    #define DIRECTINPUT_VERSION  0x0800
    #include <dinput.h>
    #include <dsound.h>
    #include <string>
    
    using std::string;
    
    #define DECLSPEC __declspec( dllexport )
    
    //Global Functions
    DECLSPEC bool RE_Init(bool windowed);
    DECLSPEC string RE_GetLastError(void);
    
    //Global Variables
    DECLSPEC LPDIRECT3D9 g_pD3D;
    DECLSPEC LPDIRECT3DDEVICE9 g_pd3dDevice;
    DECLSPEC HWND hWnd;
    
    //Private Functions
    
    //Private Variables
    string error;
    
    #endif
    Thanks for any help,
    MadCow

  2. #2
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Oh, I hate those LNK2005 errors. I used to get them all the time. I even phoned microsoft tech support. I still got them occaisnally after that. That's why I switched to Mac.

    Try a whole bunch of things. You can start off by making one general header file, and then including all the DirectX headers in there. Then use this code below to make sure that header isn't included twice:

    Code:
    #ifndef H_DIRECTXHEADERFILE
    #define H_DIRECTXHEADERFILE
    .
    . // put direct x header includes here
    .
    #endif
    If that doesn't work, try adding the directx headers to your precompiled header. That also worked for me.

    Good luck with it.

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Thanks, but the errors don't come from the dx headers. They all come from this block

    Code:
    #define DECLSPEC __declspec( dllexport )
    
    //Global Functions
    DECLSPEC bool RE_Init(bool windowed);
    DECLSPEC string RE_GetLastError(void);
    
    //Global Variables
    DECLSPEC LPDIRECT3D9 g_pD3D;
    DECLSPEC LPDIRECT3DDEVICE9 g_pd3dDevice;
    DECLSPEC HWND hWnd;
    _edit

    just quoting more errors, kind of redunant
    Basic.obj : error LNK2005: "struct IDirect3DDevice9 * g_pd3dDevice" (?g_pd3dDevice@@3PAUIDirect3DDevice9@@A) already defined in Audio.obj
    Basic.obj : error LNK2005: "struct HWND__ * hWnd" (?hWnd@@3PAUHWND__@@A) already defined in Audio.obj
    Basic.obj : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > error" (?error@@3V?$basic_string@DU?$char_traits@D@std@@V ?$allocator@D@2@@std@@A) already defined in Audio.obj
    Network.obj : error LNK2005: "struct IDirect3D9 * g_pD3D" (?g_pD3D@@3PAUIDirect3D9@@A) already defined in Audio.obj

  4. #4
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Have you got all the library files? I don't know much about D3D.

    EDIT: oh wait, are you using extern in front of your globals in the header file?
    Last edited by joeprogrammer; 03-12-2006 at 05:56 PM.

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Aha, function related errors fixed (needed to add extern before DECLSPEC)

    Now all I have is the global variables. I removed DECLSPEC from their decleration so I really don't understand why there is still trouble...I've used multiple source files before and the inclusion gaurd has always stopped them from throwing redecleration errors in each .cpp file until now it seems

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Yay! everything solved. Here's what works

    Code:
    #ifndef REAL_EARTH_ENGINE_H
    #define REAL_EARTH_ENGINE_H
    
    #include <d3d9.h>
    #define DIRECTINPUT_VERSION  0x0800
    #include <dinput.h>
    #include <dsound.h>
    #include <string>
    
    using std::string;
    
    #define DECLSPEC __declspec( dllexport )
    
    //Global Functions
    extern DECLSPEC bool RE_Init(bool windowed);
    extern DECLSPEC string RE_GetLastError(void);
    
    //Global Variables
    static LPDIRECT3D9 g_pD3D;
    static LPDIRECT3DDEVICE9 g_pd3dDevice;
    static HWND hWnd;
    
    //Private Functions
    
    //Private Variables
    static string error;
    
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 07:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 06:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 03:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21