Public Group

# 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.

## 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 on other sites
Are you using inclusion guards on your headers, so that they are only included once per source file?

##### Share on other sites
Quote:
 Original post by ScribeI 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 ScribeI 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 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 on other sites
in the cpp file bool GameStatus;
in the h file extern bool GameStatus;

##### Share on other sites
Quote:
 Original post by DerAngedin 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 on other sites
#ifndef __GLOBAL_H__
#define __GLOBAL_H__

//defines go here
#endif

##### Share on other sites
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.

##### 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 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.

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 100
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009659
• ### Who's Online (See full list)

There are no registered users currently online

×