From: cwr AT cts DOT com (Will Rose) Newsgroups: comp.os.msdos.djgpp,comp.lang.c Subject: Re: Can't get external variables to work!?? Followup-To: comp.os.msdos.djgpp,comp.lang.c Date: 7 Feb 1998 16:05:11 GMT Organization: CTS Network Services (CTSNET), San Diego, CA Lines: 57 Message-ID: <886867615.310095@optional.cts.com> References: <34DC7457 DOT 6A0A7521 AT iafrica DOT com> NNTP-Posting-Host: optional.cts.com Cache-Post-Path: optional.cts.com!cwr AT crash-i2 DOT cts DOT com To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Bruce Merry (the Almighty Cheese) (bmerry DOT nospam AT iafrica DOT com) 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): There are are several ways around this. One is: : c_types.h: : #ifndef __my_c_types_h : #define __my_c_types_h : typedef unsigned char byte; : #endif : kbd.h: : #ifndef __my_kbd_h : #define __my_kbd_h #ifndef EXTERN #define EXTERN extern #endif : #include "c_types.h" EXTERN byte keys[128]; : #endif : program.c: #define EXTERN /* defined in one source file only */ : #include "c_types.h" : #include "kbd.h" : /* stuff with the keys array */ : kbd.c: : #include "c_types.h" : static volatile byte keys[128]; /* I'm lost here; why have you declared keys[] static if you * want them to be accessible from other compilation units? * And why a volatile array? volatile is mostly used for * i/o addresses, to avoid unwanted optimisation. keys[] * is just storage (AFAIK) for data once it's been read. * Omit this line and include kbd.h to handle the declaration. * You also need to decide whether to include all headers by * name in source files, or include headers within headers, and * then stick to one convention to avoid confusion. */ : /* 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). : Thanks in advance : Bruce Will cwr AT crash DOT cts DOT com