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:
#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
#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;
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.
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.
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.
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.
You have two options for how to deal with the constants which cause the trouble.
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
.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With