From: Pete Becker Newsgroups: comp.os.msdos.djgpp,comp.lang.c Subject: Re: Can't get external variables to work!?? Date: Sat, 07 Feb 1998 11:06:51 -0500 Organization: MediaOne -=- Northeast Region Lines: 48 Message-ID: <34DC869B.33240C80@acm.org> References: <34DC7457 DOT 6A0A7521 AT iafrica DOT com> NNTP-Posting-Host: petebecker.ne.mediaone.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Bruce Merry (the Almighty Cheese) wrote: > > Hi > > I've been using C and C++ for quite a while now, but up to now I've not > needed to share a variable between source files. What I am trying at the > moment is basically (with other stuff removed): > > kbd.h: > #ifndef __my_kbd_h > #define __my_kbd_h > #include "c_types.h" > extern byte keys[128]; > #endif > > program.c: > #include "c_types.h" > #include "kbd.h" > /* stuff with the keys array */ > > kbd.c: > #include "c_types.h" > static volatile byte keys[128]; > /* code for keyboard handling */ > > Basicly, all the source files compile into objects fine, but at link > time the error 'undefined reference to keys' multiple times. What am I > doing wrong (I'm using DJGPP 2, BTW). In kbd.h it says: extern byte keys[128]; The 'extern' says that this is describing the variable named 'keys', but that it lives somewhere else. That is, it's not being defined. In kbd.c it says: static volatile byte keys[128]; The 'volatile' doesn't seem necessary from the description here, but that's not the problem. The 'static' says that this definition of keys is not to be made visible to any other file. That's why the linker complains: there's no definition of 'keys' that can be used by program.c. To fix this, remove the 'static' here. Also remove the 'volatile', unless there's something else going on here that you haven't mentioned. -- Pete