multiple definition of ""

Started by
12 comments, last by Jingo 19 years, 3 months ago
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.
Advertisement
Are you using inclusion guards on your headers, so that they are only included once per source file?
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.
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.
in the cpp file bool GameStatus;
in the h file extern bool GameStatus;
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?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
As Chacha said, have guards in your header, such as:<br>#ifndef __GLOBAL_H__<br>#define __GLOBAL_H__<br><br>//defines go here<br>#endif
In your header files, protect the file from double inclusion by using preprocessor definitions.
#ifndef MYHEADER_H#define MYHEADER_Hextern 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_Hvoid dosomething();#ifndef MYHEADER_H_PROTECT#define MYHEADER_H_PROTECTextern 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.
william bubel
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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.

This topic is closed to new replies.

Advertisement