# multiple def.

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

## Recommended Posts

hey, i've been starting to put things in different files to make it more organized and easier to maintain, but i keep getting multiple definition errors. i know why its doing it, i just can't figure out how to get around it, i have 3 or 4 header files, and 3 or 4 source files, I include the header files in one "global.h" and include that file in all my source files, is there another way to approach it?

##### Share on other sites
The problem is that then the globals.h header is included in all the source files, defining each variable each time it is included (once per source file). This is where you get multiple definitions. Try using this:
#ifndef GLOBALS_H#define GLOBALS_H//content of the file#endif

What that does is check if the file has already been included, and if it has, it won't include it again, therefore ridding you of your multiple definitions. Just be sure that you add that stuff at the beginning, then at the end have the #endif. Oh, and replace GLOBALS_H according to the name of the header file.

##### Share on other sites
You may also need to make a globals.cpp to go with globals.h.
If you have any variable definitions in your headers, than you are going to run into issues at link time, because
each variable will delcared for each source.
But, if you just move all those variables to globals.cpp, and just leave 'extern' definitions in the headers,

##### Share on other sites
oh ok thanks alot, one clarification, you put the #endif at the end of the file? or at the end of the "includes"?

by the way, thanks alot for the help

##### Share on other sites

EDIT: And the #endif goes at the end of the .h, as bschneid said.

##### Share on other sites
hmm, its still being difficult, by the way its not a compile error, its a linker error, maybe this will help

//this will be the file that is included in the cpp's
//this will have all global variables, and a list of includes

// our includes
#ifndef global_h
#define global_h

#include <cstdlib>
#include <string>
//#include "sdlsetup.h"
//SDL includes
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "marioclass.h"

using namespace std;

//our global variable definitions

SDL_Event event;
Mario mario;
const int screen_width = 640;
const int screen_height = 480;
const int screen_bpp = 16;
int bgX = 0;
SDL_Surface* screen = NULL;
SDL_Surface* bg = NULL;
SDL_Surface* marioimage[5] = {NULL,NULL,NULL,NULL,NULL};

//our function declarations

void init();
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
void cleanup();

void showscreen();

void showscreen()
{

apply_surface(bgX,0,bg,screen);
mario.show();

SDL_Flip(screen);

}

#endif

##### Share on other sites
ok, i narrowed it down to the chunk of variable definitions

SDL_Event event;
Mario mario;
const int screen_width = 640;
const int screen_height = 480;
const int screen_bpp = 16;
int bgX = 0;
SDL_Surface* screen = NULL;
SDL_Surface* bg = NULL;
SDL_Surface* marioimage[5] = {NULL,NULL,NULL,NULL,NULL};

when i put it in a cpp source file, it says its not defined, when i put it in the global.h , there's a link time multiple def. error

##### Share on other sites
sorry for all the posts, i reread all the posts and realized i missed KulSeran's advice, i tried it and it worked, but i can't call extern for objects can I? I tried and it won't let me, so for the object of my class, and object of SDL_Event, how woudl i do that? those are the only 2 holding me up

##### Share on other sites
Quote:
 Original post by godsenddeathbut i can't call extern for objects can I? I tried and it won't let me

1) 'extern' is a keyword, not a function, so you don't "call" it. You can "use" it, or "extern" variables (treating the keyword as a verb).

2) What *exactly* did you try? There's nothing different about how C++ handles objects versus primitives here.

• 11
• 9
• 11
• 9
• 12