static variables in two different files

Hi, all, I have a question about static variables:
I accidently defined two static variables of the same name in two different files:

1
2
3
4
5
//file1.h
static double AAA=1.111;
{
... ...
}

and
1
2
3
4
5
//file2.h
static double AAA=1.111;
{
... ...
}

I thought there should be no problem about this, since these static variables above are just visible in their own files not from outside files, but the compiler says:
----------------------------------------------------------
file1.h:1: error: redefinition of `double AAA'
file2.h:1: error: `double AAA' previously defined here
: confused by earlier errors, bailing out
----------------------------------------------------------
could anybody do me a favor and explain why?
Thanks a lot
If you include both file1.h and file2.h in the same translation unit - then you will
get the redefinition error (and it look like you are doing just that)
Thanks, guestgulkan:
but isn't it true that a static variable defined at the beginning of a file outside of any { ..} should be just visible to that file only, then how could another file detect a conflict with this? Thanks
Well, not to be a smart a55, but I'd say that your static variables are not local to their individual files.

You might be able to avoid the problem with the use of the preprocessor directive ifndef.

Here is an example of how to use it:

1
2
3
#ifndef AAA        // If AAA is undefined
#define AAA 1.111  // Define AAA
#endif 


This does not create a static double but causes the pre-processor to do a "search and replace" throughout your code and actually replaces "AAA" with the literal "1.111" before it sends it off to the compiler.

You would put this in both files, so no matter which is loaded second it only gets defined once.

Here is a link, ifndef is about half way down.
http://www.cplusplus.com/doc/tutorial/preprocessor/
for the love of god don't use macros.

static variables are only visible to the same source file not to the same file.

Remember that #include is basically a glorified copy/paste. When you include a header, the compiler basically copies the header and pastes it in the cpp file. So when you include file1.h from myfile.cpp, myfile.cpp defines the static double AAA.

Then when you include file2.h from myfile.cpp, myfile.cpp defines it again hence the error.


As for a solution -- it's an organizational thing. Typically, globals are evil and should be avoided (not only for this reason, but for many other reasons). I would suggest you rethink your design and ask yourself if:

1) these two variables need to be global
2) do they need to be named the same thing (seriously, wtf?)

or if they're meant to be the same variable, why in Gods name are you declaring it twice?
1) these two variables need to be global

then apply extern keyword at the second file when you declared those global variable declared at first file.
Thank you all, guys:
as i said, I accidently defined these two variables with the same name, I already fixed the issue before coming here, but I was just not sure why the previous error happens. should be more careful about using global variables.
as i said, I accidently defined these two variables with the same name,


Just one more reason why globals should be avoided.

should be more careful about using global variables.


Or better yet.. don't use them at all ;P


I need to get around to writing an article of why globals are evil.
Topic archived. No new replies allowed.