On Wed, Oct 24, 2007 at 08:24:06PM -0500, Mark Rages wrote: > I define as extern in the header and as static in the C file. Works > fine on the compilers I've used. That got my Spidey sense tingling. So I had to try it. gcc 4.0.4 on a Debian Linux box. Setup: ------------ a.c --------------- #include #include "s.h" static int junk; main() { junk = 1; printf("Junk = %d\n",junk); b(); printf("Junk = %d\n",junk); } -------------------------------- ------------ b.c ---------------- #include "s.h" b() { junk = 2; } --------------------------------- ------------- s.h --------------- extern int junk; b(); --------------------------------- My gut says that the junk declared in a.c is isolated to a.c so b.c can't see it. Since s.h only has an extern, then b.c has no real definition for junk. First off the compiler outsmarted me. I did the standard include along with the declaration in a.c. The compiler didn't like that: -------------------- cc -c -o a.o a.c In file included from a.c:2: s.h:2: warning: data definition has no type or storage class a.c:3: error: static declaration of 'junk' follows non-static declaration s.h:1: error: previous declaration of 'junk' was here -------------------- Good compiler. It knows that these are two different references. Just for giggles I dropped the include in a.c and tried again. I got the expected: -------------------- cc -o total a.o b.o b.o: In function `b': b.c:(.text+0x5): undefined reference to `junk' collect2: ld returned 1 exit status make: *** [total] Error 1 -------------------- So b.o compiled as expected with an external reference to junk. However the linker couldn't find a reference to match it. Now fixing the problem by dropping the static and readding the include in a.c produces: ------------------------ cc -c -o a.o a.c cc -c -o b.o b.c cc -o total a.o b.o $ total Junk = 1 Junk = 2 ------------------------ Which is the shared expected result. Static isolates. Static eliminates sharing. So if your compiler is sharing variables that are declared as static, then something is wrong. BAJ -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist