Sign in to follow this  

multiple definition of ""

This topic is 4726 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
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
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
Quote:
Original post by Raduprv
... and it's a pain in the ass to include all the needed header files ...


A small pain in the ass is better than a big pain in the ass. For example, what happens when you use the same file in two different projects? Which global header file does it include? Also, in a large project, you will have hundreds of header files. If every file has to include hundreds of header files, compiling will take forever.

Share this post


Link to post
Share on other sites
We use some header files in our client and map editor. The problem is solved by using #defines.
For example, there is a define for the client, and one for the map editor, and the headers will load only the necesary stuff.
Others do it too, just look in windows.h

Share this post


Link to post
Share on other sites
Quote:
Original post by Inmate2993
In your header files, protect the file from double inclusion by using preprocessor definitions.
*** Source Snippet Removed ***

*** Source Snippet Removed ***

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

*** Source Snippet Removed ***

*** Source Snippet Removed ***

Its one of many ways to do it.


I would just like to mention a game programming tip I picked up from a book - if you have a larger project, you will want to do the same thing with the #ifndef, but Also in the source files. This will cut down compile time a lot because instead of pasting the whole header file it will skip it.

Here is an example:

header1.h:
#ifndef h1
#define h1
...
#endif

file1.cpp:
#ifndef h1
#include "header1.h"
#endif
...

.....................

file999.cpp:
#ifndef h1
#include "header1.h"
#endif
...

Instead of always pasting the entire header1.h file in every source file - then not even compiling it because it is already defined - you can use this method to only compile it once and include it once, thus saving a lot of compile time. One thing htey mentioned that is does look messy - but its valuable. You can also do the same thing for standard includes as well.

- Drew

Share this post


Link to post
Share on other sites
Header guards should make no difference in the case that the OP described. All that would happen without them is he would get multiple declarations, not multiple definitions, which is fine.

Share this post


Link to post
Share on other sites

This topic is 4726 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this