Sign in to follow this  

including globals gives duplicate warning

This topic is 4688 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

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 this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4688 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