Path: utzoo!attcan!uunet!pyrdc!pyrnj!rutgers!att!mtunb!dmt From: dmt@mtunb.ATT.COM (Dave Tutelman) Newsgroups: comp.sys.ibm.pc Subject: Re: Possible Bug in Turbo Linker (and question about Turbo Debug) Message-ID: <1325@mtunb.ATT.COM> Date: 16 Nov 88 23:29:41 GMT References: <1254@helios.ee.lbl.gov> Reply-To: dmt@mtunb.UUCP (Dave Tutelman) Organization: AT&T Bell Labs - Lincroft, NJ Lines: 60 In article <1254@helios.ee.lbl.gov> forrest@ux1.lbl.gov (Jon Forrest) writes: >... >if you declare the same external variable in more than one file, >the linker treats them as separate variables instead of treating >them as references to the same variable. You can see this be >creating 2 files, each with "int x" as the first line. Compile >them and link them. You'll see an error message from the linker >saying "Error: x defined in module YYY is duplicated in module ZZZ" >(where YYY and ZZZ are the names of the files). >.... On the other >hand, if you say "extern int x" in all but one of the files referencing >"x", and then "int x" in just one file, then everything works OK. > >The reason I think this is wrong, is that K&R (1st edition) >on page 206, section 11.1 says "... all references to the same >external identifier refer to the same object ...". Please re-read that section. It states very clearly: "Thus in a multi-file program, an external data definition without the 'extern' specifier must appear in exactly one of the files. Any other files which wish to give an external definition for the identifier must include the 'extern' in the definition." Turbo is implemented correctly, though I have encountered one compiler that takes the more convenient tack you would prefer. (Sorry, don't remember which one.) >Needless to say, this is a pain when such definitions are done in an >#include file. Not necessarily. My template for an #include file starts with: #ifdef MAIN # define EXTERN # define INIT(x) =x #else # define EXTERN extern # define INIT(x) #endif Then I use EXTERN (rather than extern) for all external variables I define in the header file. I make sure exactly one of my source files contains an early #define MAIN (before my #include). Example of definitions in my header file: EXTERN int i; EXTERN int j INIT(3); In MAIN, this expands to: In other files this expands to: int i; extern int i; int j =3; extern int j; Hope this helps. +---------------------------------------------------------------+ | Dave Tutelman | | Physical - AT&T Bell Labs - Lincroft, NJ | | Logical - ...att!mtunb!dmt | | Audible - (201) 576 2442 | +---------------------------------------------------------------+