Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variables in Header file causing redefinition error c++

I have a header file which holds all my global variables(and a cpp file to declare them) and I use the #ifndef #define #endif tags, but I still get redefinition errors

I have a total of 3 header files and 4 cpp files, and all of the header / main.cpp contains the globalvar.h header file.

Here is the code:

GlobalVar.h

#ifndef GLOBALVAR_H
#define GLOBALVAR_H
#include "SDL.h"

extern const int SCREEN_WIDTH = 960;
extern const int SCREEN_HEIGHT = 960;
extern const int SCREEN_BPP = 32;

extern const int FRAMES_PER_SECOND = 30;
//tiles attribute
extern const int TILE_WIDTH = 64;
extern const int TILE_HEIGHT = 64;
extern const int TOTAL_TILES = 150;
extern const int TOTAL_SPRITES = 64;
//tile sprites
extern SDL_Rect clip[144];
//Images / backgrounds
extern SDL_Surface* screen;
extern SDL_Surface* background;
extern SDL_Surface* Ike;
extern SDL_Surface* thetiles;
extern SDL_Event event;

#endif

GlobalVar.cpp

#include "GlobalVar.h"

const int SCREEN_WIDTH = 960;
const int SCREEN_HEIGHT = 960;
const int SCREEN_BPP = 32;

const int FRAMES_PER_SECOND = 30;
//tiles attribute
const int TILE_WIDTH = 64;
const int TILE_HEIGHT = 64;
const int TOTAL_TILES = 150;
const int TOTAL_SPRITES = 64;
//tile sprites
SDL_Rect clip[144];
//Images / backgrounds
SDL_Surface* screen;
SDL_Surface* background;
SDL_Surface* Ike;
SDL_Surface* thetiles;
SDL_Event event;
like image 940
user986463 Avatar asked Oct 09 '11 15:10

user986463


People also ask

Can we declare global variable in header file in C?

ANSWER. Yes. Although this is not necessarily recommended, it can be easily accomplished with the correct set of macros and a header file. Typically, you should declare variables in C files and create extern definitions for them in header files.

How do you solve redefinition error?

Re “How do you fix a redefinition error in C programming?”, you do that by removing the redefinition. Compilers can get confused when subjected to unexpected repetition in source code. So when they complain about it, the fix is to find and remove the repetition.

How do you define a global variable in a header?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.

Can we declare same variable in two times globally?

You must define all variables. In C, a definition of a global variable can be used for a declaration multiple times. But if the program only has extern int x; , which is a declaration, the compile will abort since there is no place where memory is allocated to the variable.


2 Answers

You have two options for how to deal with the constants which cause the trouble.

Option 1

Remove the extern from the header:

#ifndef GLOBALVAR_H
#define GLOBALVAR_H
#include "SDL.h"

const int SCREEN_WIDTH = 960;
const int SCREEN_HEIGHT = 960;
const int SCREEN_BPP = 32;

const int FRAMES_PER_SECOND = 30;
const int TILE_WIDTH = 64;
const int TILE_HEIGHT = 64;
const int TOTAL_TILES = 150;
const int TOTAL_SPRITES = 64;

extern SDL_Rect clip[144];
extern SDL_Surface* screen;
extern SDL_Surface* background;
extern SDL_Surface* Ike;
extern SDL_Surface* thetiles;
extern SDL_Event event;

#endif

If you do this, you must not define the variables in GlobalVar.cpp.

Option 2

Remove the initializers from the header:

#ifndef GLOBALVAR_H
#define GLOBALVAR_H
#include "SDL.h"

extern const int SCREEN_WIDTH; // = 960;
extern const int SCREEN_HEIGHT; // = 960;
extern const int SCREEN_BPP; // = 32;

extern const int FRAMES_PER_SECOND; // = 30;
extern const int TILE_WIDTH; // = 64;
extern const int TILE_HEIGHT; // = 64;
extern const int TOTAL_TILES; // = 150;
extern const int TOTAL_SPRITES; // = 64;

extern SDL_Rect clip[144];
extern SDL_Surface* screen;
extern SDL_Surface* background;
extern SDL_Surface* Ike;
extern SDL_Surface* thetiles;
extern SDL_Event event;

#endif

Now you do need to define and initialize the constants in GlobalVar.cpp.

This disadvantage of this is that you cannot use names such as SCREEN_WIDTH in contexts that require a compile-time integer constant, such as the dimensions of an array or the case clauses of a switch statement.

So, option 1 is the technique which is used more often.

like image 185
Jonathan Leffler Avatar answered Sep 19 '22 16:09

Jonathan Leffler


You should only give the constants values in one place.

You an either keep the extern declarations in the header (without the values) and have the values in the cpp file, or remove the extern keyword and define the values in the header only.

like image 37
Bo Persson Avatar answered Sep 21 '22 16:09

Bo Persson