• 13
• 18
• 19
• 27
• 10

# global variables in a header file

This topic is 4785 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a header file named "Globals.h" which I use to store a few Direct X variables: LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices I also have a header and cpp file named "Graphics" and also "Render". The problem I have is that both "Graphics" and "Render" need to use "Globals.h". The problem with this is that "Render.cpp" needs to #include "Graphics.h" in order to use a class....The point I am trying to make is that when I #include "Graphics.h" into my "Render.cpp" file, I get linker errors because "Graphics.cpp" also #includes "Graphics.h". Both .cpp files are trying to define the global variables giving me linker errors. Is there any way to work around this? "Graphics.h"----#include "Globals.h" "Graphics.cpp"-----#include "Graphics.h" "Render.cpp"-----#include "Graphics.h"

#pragma once
in the Globals.h

##### Share on other sites
Do the following:

Globals.h

#ifndef GLOBALS_H#define GLOBALS_Hextern int iExampleVar;#endif

Globals.cpp

#include "Globals.h"int iExampleVar = 0;

##### Share on other sites
hmm...that didn't seem to work. I still get link errors.
I'll post all of my code if you want me to.

##### Share on other sites
Never mind. I posted before I saw your post Hunta. I did this and it fixed it:

//Globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
#pragma once

#include "Includes.h"

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
extern LPDIRECT3D9 g_pD3D; // Used to create the D3DDevice
extern LPDIRECT3DDEVICE9 g_pd3dDevice; // Our rendering device
extern LPDIRECT3DVERTEXBUFFER9 g_pVB; // Buffer to hold vertices

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color; // The vertex color
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

#endif

//Globals.cpp
#include "Globals.h"

LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;

##### Share on other sites
one last question. how do I insert code into the little scroll windows when I post it?

##### Share on other sites
On this FAQ scroll down to the "How do I put links/quotes/images/code/smileys in my posts?" section and it gives you lots of options! You can even see some neat smileys to use as well [wink]. Insert your code between the ] and [ of the tags.

One more thing you can alos do is simply click edit on someone's post and see the code that they used.

- Drew