#include woes - #ifndef not working?

Started by
2 comments, last by LonelyTower 22 years, 2 months ago
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
"Magic makes the world go round." - Erasmus, Quest For Glory I
Advertisement
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
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 fileextern int MyInt;// in main.cppint 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.
Hey, thanks!
"Magic makes the world go round." - Erasmus, Quest For Glory I

This topic is closed to new replies.

Advertisement