Jump to content
  • Advertisement
Sign in to follow this  
godsenddeath

multiple def.

This topic is 4140 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

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


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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,
than you have solved your problem, and made your life easier, cauze you can easily find all your globals.

Share this post


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


Link to post
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();
SDL_Surface* load_image(string filename);
void load_files();
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 this post


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


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


Link to post
Share on other sites
Quote:
Original post by godsenddeath
but 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!