Sign in to follow this  

global variables in a header file

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

If you intended to correct an error in the post then please contact us.

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"

Share this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this