Archived

This topic is now archived and is closed to further replies.

LonelyTower

#include woes - #ifndef not working?

Recommended Posts

Hi here, here's the situation that I am being bugged with. I have 2 CPP files, 1 header file. The .H file defines functions and have a couple of lines that that defines a couple of global variables:
    
LPDIRECT3D8 g_pD3D = 0; 
LPDIRECT3DDEVICE8 g_pDevice = 0;
D3DPRESENT_PARAMETERS g_SavedPresParams;
LPDIRECT3DSURFACE8 g_pBackSurface;
int g_DeviceHeight;
int g_DeviceWidth;
CCamera g_Camera;
  

The 2 CPP files (one is engine.cpp and the other is core.cpp) both included the header file above. I have put

      
#ifndef CLASSDEFINE_H
#define CLASSDEFINE_H

.
.
LPDIRECT3D8 g_pD3D = 0; 
LPDIRECT3DDEVICE8 g_pDevice = 0;
D3DPRESENT_PARAMETERS g_SavedPresParams;
LPDIRECT3DSURFACE8 g_pBackSurface;
int g_DeviceHeight;
int g_DeviceWidth;
CCamera g_Camera;

#endif
  
But i still end up with the folowing complie error:
quote:
inking... engine.obj : error LNK2005: "struct IDirect3DDevice8 * g_pDevice" (?g_pDevice@@3PAUIDirect3DDevice8@@A) already defined in core.obj engine.obj : error LNK2005: "struct IDirect3D8 * g_pD3D" (?g_pD3D@@3PAUIDirect3D8@@A) already defined in core.obj engine.obj : error LNK2005: "struct IDirect3DSurface8 * g_pBackSurface" (?g_pBackSurface@@3PAUIDirect3DSurface8@@A) already defined in core.obj engine.obj : error LNK2005: "struct _D3DPRESENT_PARAMETERS_ g_SavedPresParams" (?g_SavedPresParams@@3U_D3DPRESENT_PARAMETERS_@@A) already defined in core.obj engine.obj : error LNK2005: "int g_DeviceHeight" (?g_DeviceHeight@@3HA) already defined in core.obj engine.obj : error LNK2005: "class CCamera g_Camera" (?g_Camera@@3VCCamera@@A) already defined in core.obj engine.obj : error LNK2005: "int g_DeviceWidth" (?g_DeviceWidth@@3HA) already defined in core.obj Debug/Direct3D_GameEngine.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. Direct3D_GameEngine.exe - 8 error(s), 0 warning(s)
So what did I do wrong? Edited by - LonelyTower on January 30, 2002 9:47:59 AM

Share this post


Link to post
Share on other sites
The problem is that since you are declaring the data items in the header files, then including them in two source files, you have two instances of those items, each with the same name.

You need to use the "extern" keyword on variables you wish to share this way, and then define them in ONE of the source files. Like so:

// header
extern LPDIRECT3D8 g_pD3D;

// first source
LPDIRECT3D8 g_pD3D;

// every other source that needs it
#include <header>

Does that make sense?

Good luck!
Bill

Share this post


Link to post
Share on other sites
You can''t actually define global variables in a shared header file. You can declare them with the extern keyword and then define them in *one* cpp though...

  
// shared header file


extern int MyInt;

// in main.cpp


int MyInt = 0;


You can then use MyInt in any file that includes the header file, but it only actually gets created when main.cpp is built.

Share this post


Link to post
Share on other sites