including globals gives duplicate warning

Started by
5 comments, last by Madster 19 years, 2 months ago
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]
Working on a fully self-funded project
Advertisement
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.
I am the master of ideas.....If only I could write them down...
Thanks for the reply.

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?
Working on a fully self-funded project
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
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...
I am the master of ideas.....If only I could write them down...
Quote:Original post by Nathaniel Hammen
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


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;}
That worked. I must say you all rock. Thanks!

To anyone reading this post, remember rebuilding all the objects that are being changed. Sometimes the compiler could use the cached ones and give incorrect errors. If the project is small, better rebuild all until you get dependencies right.
Working on a fully self-funded project

This topic is closed to new replies.

Advertisement