• 12
• 12
• 9
• 10
• 13

including globals gives duplicate warning

This topic is 4785 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, this is probably standard stuff, but seems my programming classes were superficial and i'm left to discover stuff like this on my own. I have three files like the following ones. Upon compiling, i get "multiple definition error" for 'verbose' and anything i try to define with it (global) Framework.cpp -------------
#include "Framework.h"
Framework.h -----------
#ifndef _FRAMEWORK_H
#define _FRAMEWORK_H

bool verbose;

#endif
main.cpp --------
#include "Framework.h"

int main(int argc, char **argv)
{
verbose = false;
return 0;
}
The question is... why? shouldn't the #ifndef prevent duplicate inclusion of Framework.h ? HALP! edit: made code less annoying, smaller post now [Edited by - Madster on February 12, 2005 12:33:41 AM]

Share on other sites
the ifndef is for each cpp file. This means that if you did #include "Framework.h" in another include file, and the included both framework.h and this other file in main, framework.h would only be included once.

What you need to do, is change the bool verbose in Framework.h int extern bool verbose. There should only be one file (presumably Framework.cpp) that doesn not use the extern.

Share on other sites

If i make it
extern bool verbose;

Then i get 'undefined reference to verbose' as expected, since now its not defined anywhere.

[bawling] i hate these bugs.
A friend suggested i just stuff everything in a class and make everything static in it.
He also showed that i could declare verbose as extern and have an extra inlcude file with globals that should be included from each file that includes Framework.h

And its as an ugly fix as it sounds. Are there any proper ways to stuff global variables and functions in a separate file?

Share on other sites
Once you've externed the variable, you have to define it in one location where it is declared (ie: Framework.h has been included). So, in main.cpp, you could just add

bool verbose;

outside of your main function and now the variable will be available in every file that includes Framework.h.

If you were to make static class members, you'd have to do effectively the same thing (but instead type bool classname::verbose;, so it's up to you how you want to do it.

-Auron

Share on other sites
Maybe I wasn't clear when I said that there should be one file that does not use the extern... Anyways, Auron desribed it perfectly... Although, since the variable is included in Framework.h, I would put the declaration of it in Framework.cpp

Sorry if I appear to be rude, but it's 1 am here, and I didn't get much sleep last night... speaking of which, it's time to do so...

Share on other sites
Quote:
 Original post by Nathaniel HammenMaybe I wasn't clear when I said that there should be one file that does not use the extern... Anyways, Auron desribed it perfectly... Although, since the variable is included in Framework.h, I would put the declaration of it in Framework.cpp

Example:

Verbose.hh :extern bool verbose;Verbose.cc :bool verbose;Source1.cc :#include "Verbose.hh"void do_something_1( void ){    verbose = true;}Source2.cc :#include "Verbose.hh"void do_something_2( void ){    verbose = false;}