# "global redefined" - C++

## Recommended Posts

I've got this "global" library for my project, which will include all the standard includes and define all the "project-wide" globals, but I have a problem: when I try to add a global variable in, VC++, and I include the globals library in more than one file, I get this "globals redefined". Here is my code:
#ifndef __GLOBALS__
#define __GLOBALS__

#include "DarkGDK.h"
#include "d3dfunc.h"

#define SWIDTH  600
#define SHEIGHT 600
#define SDEPTH  32

struct Globals
{
bool end;
} globals;

#endif

I've also tried
#pragma once
, but that didn't work either. How to I declare these globals?

##### Share on other sites
Try this (just guessing...) in the header file...

struct Globals
{
bool end;

};

extern Globals globals;

This defines what a Globals structure looks like rather than creating one.

Then you'll need to create a Globals structure once in a cpp file...

Globals globals;

Or just avoid globals altogether :)

##### Share on other sites
But I need my globals to be usable in most of my core cpp files - I can't redefine them in each file, it just wouldn't work...

##### Share on other sites
Quote:
 Original post by Element of PowerBut I need my globals to be usable in most of my core cpp files - I can't redefine them in each file, it just wouldn't work...

Problem #4 as gumpy linked explains how to do that. Declare your globally accessible variables as extern and then define them in exactly one source file, instead of defining them in the header file. It's the multiple definition that is causing your error.

##### Share on other sites
You should also be aware that such "global" headers can introduce subtle compiler errors if they are including a lot of your own files, and can also drastically increase compile times, if implemented incorrectly.

##### Share on other sites
Quote:
 Problem #4 as gumpy linked explains how to do that. Declare your globally accessible variables as extern and then define them in exactly one source file, instead of defining them in the header file. It's the multiple definition that is causing your error.
Ah, I didn't realise there are multiple pages. I'll try that.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628282
• Total Posts
2981812

• 9
• 10
• 11
• 17
• 15