On Wed, Oct 24, 2007 at 04:28:16PM -0700, James Newton wrote: > Ok, I figured this out on my own, after writing the post I tried the other > guys "static" keyword in my main.h file and that worked. I'm going to post > it anyway 'cause I like looking stupid... Err... No, I'm going to post it > because some day some other poor soul may have the same confusion. Had I > posted it, some kind soul would have replied "RTFM: static keyword, main.h" > Here is the original post. > > The is undoubtedly a stupid question, but I've managed to confuse myself > enough that I need some help.. > > I'm working on probably the largest program I've ever done in C. There are 5 > separate ".C" files and double that in ".H" files. Some of the C files are > being done by other people. I have main.c and .h, and another programmer is > doing flash.c and .h. My main, of course, includes flash.h as well as main.h > and his flash.c also includes both of those as well. That's SOP. > We need to access some "global" variables between us. E.g. a pair of count > down timer variables and a status flag byte. I have to decrement the timers > in the interrupt routine that I wrote and he sets those and waits for them > to empty at points in his code. We each set and reset flags in the status > byte and branch based on their settings. > > Where and how do we define those variables? You can only define them once. You then have to reference them multiple times. > The chip we are using is an MSP430F1611 and the compiler is IAR. This is a standard C issue. Compiler and chip shouldn't matter. > > My understanding is that they should be declared in main.h so that the > declaration is seen in both places, but when I do that, it compiles ok, but > the linker complains that > " Error[e27]: Entry "Timer1" in module main ( > D:\user\james\code\JAMES-WIP\msp430\logger\Debug\Obj\main.r43 ) redefined in > module flash ( D:\user\ > james\code\JAMES-WIP\msp430\logger\Debug\Obj\flash.r43 ) " Correct on the error. Each of the two objects thinks that it has its own copy of the variables, and the linker whines about conflicting definitions. > > This is the section of main.h: > > volatile > BYTE Stat = STAT_NO; // status > > volatile > BYTE Timer1, Timer2; // 10mS decrement timer Nope. Bad move. The rule is that one never actually defines a variable in a header file for exactly the reason outlined above. > I think there must be come compiler setting that I'm missing that would make > it understand that those "two" variables are one and the same. Yup. But it's not possible to declare that in a single place. > The guy writing FLASH.H seems to think they should only be defined in his > code, That could work. But not in FLASH.H. FLASH.C. > but says that using the "static" keyword will somehow make the values > available in main. Nope. Exactly the opposite. static states that the variable is local to the file declaring it and that no outside code can get access. > When we do that, the compiler complains that " > Error[Pe020]: identifier "Timer1" is undefined > D:\user\james\code\JAMES-WIP\msp430\logger\main.c 77" and so on for the > other variables. Bingo. So here's that answer to the homework problem. When two separately compiled code sections in C need to share a variable, one code section declares and owns the variable, while the other simply references it using the header file. This means that the variable in fact needs to be declared twice: once in the code, and once in the header. So something like: --------- FLASH.C ------------- #include "FLASH.H" // This is OK even though a second declaration exists // The real definition overrides the extern. volatile BYTE Stat = STAT_NO; // status // This is the real and only definition of the variable ------------------------------- --------- FLASH.H ------------- extern volatile BYTE Stat = STAT_NO; // status // The extern indicates that the variable is defined elsewhere and that the // linker should go find that definition in another compiled object. ------------------------------- --------- MAIN.C -------------- #include "FLASH.H" // includes the extern declaration so MAIN.C can use the // variable without having to declare it itself ------------------------------- Note that no actual definition/declaration of the variable occurs in MAIN.C except via the included declaration in FLASH.H. This way updates to the declaration/definition of the variable only needs to occur in two places instead of everywhere that the variable is used. Hope this helps, BAJ (Who used to teach C for several years) -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist