Jump to content
  • Advertisement
Sign in to follow this  
Scribe

multiple definition of ""

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

Hi guys! Erm, yer, I'm new to c++ coding and i think i'll mention now i hate Classes and tbh they're not very relavent to my current project. So plz dn't mind the fact i'm not using them. I have a lot of source files, all of which i want to be able to use a few global variables. So i have the header file 'global_data.h' contaning includes to all other header files and also holding my global data in the form: extern bool GameStatus; I then have a c++ file called 'global_data.cpp' that includes 'global_data.h' and in which i have my data actually being declared in the form: bool GameStatus; Which is outside any functions. My other headers and main.cpp all include 'global_data.h'. All headers are correctly wrapped. I get multiple definition errors on ALL of my global variables lol. Please help. Thanks. BTW i'm using Dev C++ 4.9.9.1 along with some SDL extentions.

Share this post


Link to post
Share on other sites
Advertisement
Are you using inclusion guards on your headers, so that they are only included once per source file?

Share this post


Link to post
Share on other sites
Quote:
Original post by Scribe
I get multiple definition errors on ALL of my global variables lol.

What you described should work. As long as you are externing them in translation units that need to access them and only defining them in exactly one cpp file, then you shouldn't get any link errors. Check the exact error and see if it mentions which translation units have the definitions and then see if you accidently defined them in those additional files as well.

Quote:
Original post by Scribe
I have a lot of source files, all of which i want to be able to use a few global variables. So i have the header file 'global_data.h' contaning includes to all other header files and also holding my global data in the form:

...

My other headers and main.cpp all include 'global_data.h'.

I know you're not going to want to hear it, but you should definately rethink your design. Having one file which includes all other header files which is included by every translation unit is not a good idea as it is just going to unnecessarily inflate your build times (each of those cpp files has to parse those headers separately). If you are very stubborn about this, at least look into using precompiled header files if your compiler supports them.

Share this post


Link to post
Share on other sites
Sounds about right, maybe post the code?

Make sure you are not doing anything like extern int i = 10; in the header file, as the initializer turns it into a definition.

Share this post


Link to post
Share on other sites
Quote:
Original post by DerAnged
in the cpp file bool GameStatus;
in the h file extern bool GameStatus;


As the others have said, this should work. Can you post the actual code, so that we my look for what you might have gotten wrong?

Share this post


Link to post
Share on other sites
As Chacha said, have guards in your header[s], such as:
#ifndef __GLOBAL_H__
#define __GLOBAL_H__

//defines go here
#endif

Share this post


Link to post
Share on other sites
In your header files, protect the file from double inclusion by using preprocessor definitions.

#ifndef MYHEADER_H
#define MYHEADER_H

extern int worthsomething;
void dosomething();

#endif /* MYHEADER_H */




#include "myheader.h"
#include "myheader.h"
#include "myheader.h"
#include "myheader.h"
#include "myheader.h"

int main( int argc, char ** argv )
{
worthsomething = 5;
dosomething();
return 0;
}



However, if you want everything to work from a single include file, you have to do this:


#ifndef MYHEADER_H
#define MYHEADER_H

void dosomething();

#ifndef MYHEADER_H_PROTECT
#define MYHEADER_H_PROTECT
extern int worthsomething;
#endif

#endif /* MYHEADER_H */




#define MYHEADER_H_PROTECT
#include "masterheader.h"

int worthsomething;

void dosomething()
{
worthsomething = 2993;
}



Its one of many ways to do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Scribe
... So i have the header file 'global_data.h' containing includes to all other header files and also holding my global data ...


This is poor programming. It generally indicates a lack of design and understanding. It might not be a problem for a small project, but on a large project it will lead to many problems.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Quote:
Original post by Scribe
... So i have the header file 'global_data.h' containing includes to all other header files and also holding my global data ...


This is poor programming. It generally indicates a lack of design and understanding. It might not be a problem for a small project, but on a large project it will lead to many problems.


Not really. We are using this method in our server and client, and works quite well. If you have a large project, most likely some headers will depend on other headers, and it's a pain in the ass to include all the needed header files in each file. So you just include the globl one, which contains the others.

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!