home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4668 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  55 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: chang.unx.sas.com!walker
  3. From: walker@chang.unx.sas.com (Doug Walker)
  4. Subject: Re: SasC linker Error:
  5. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  6. Message-ID: <Dnnssy.26K@unx.sas.com>
  7. Date: Sat, 2 Mar 1996 21:24:34 GMT
  8. X-Nntp-Posting-Host: chang.unx.sas.com
  9. References: <dkauppDnEzzs.FsD@netcom.com> <758.6631T1348T796@sn.no> <4h0tsv$dsi@strauss.udel.edu>
  10. Organization: SAS Institute Inc.
  11.  
  12. In article <4h0tsv$dsi@strauss.udel.edu>,
  13. John Pat Corigliano <jcorig@strauss.udel.edu> wrote:
  14. >I like to use one header file that contains the declarations of all global
  15. >variables.  The "trick" to doing this is to create a macro like so:
  16. >
  17. >#ifdef MAIN_C
  18. >#define Global
  19. >#define Init(var, val) var = val
  20. >#else
  21. >#define Global extern
  22. >#define Init(var, val) var
  23. >#endif
  24.  
  25. This will not work if you use GSTs with your SAS/C project.  You cannot
  26. define data in a header file, only declare it.
  27.  
  28. I recommend using an "extern" declaration in a header file, then picking
  29. one .c file and adding a second declaration without the extern keyword.
  30. You can then add an initializer if you like.  Assuming the .c file
  31. includes the .h file, you'll get a compiler warning if you ever forget
  32. to keep the definition and the declaration in sync.
  33.  
  34. BTW, your use of "declare" and "define" is sloppy.  A function or data
  35. item can only be DEFINED once, but it may be DECLARED multiple times.
  36. A definition always serves as a declaration as well.
  37.  
  38. A trick of ANSI C, by the way, is that the following is actually a 
  39. DEFINITION, not a declaration, and will cause multiple definition
  40. problems if found in a header file:
  41.  
  42.    extern int x = 10;
  43.    
  44. The initializer converts what would otherwise have been a declaration
  45. into a definition.
  46.  
  47. -- 
  48.   *****                    / walker@unx.sas.com
  49.  *|_o_o|\\     Doug Walker<  BIX, Portal: djwalker
  50.  *|. o.| ||                \ AOL: weissblau
  51.   | o  |//     
  52.   ====== 
  53. Any opinions expressed are mine, not those of SAS Institute, Inc.
  54.  
  55.