home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / os / vms / 19273 < prev    next >
Encoding:
Internet Message Format  |  1992-12-14  |  3.6 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!nntp-server.caltech.edu!eql.caltech.edu!rankin
  2. From: rankin@eql.caltech.edu (Pat Rankin)
  3. Newsgroups: comp.os.vms
  4. Subject: External objects in C
  5. Followup-To: comp.os.vms
  6. Date: 14 Dec 1992 19:35 PDT
  7. Organization: California Institute of Technology
  8. Lines: 52
  9. Distribution: world
  10. Message-ID: <14DEC199219350160@eql.caltech.edu>
  11. References: <PRZEMEK.92Dec9100328@rrdstrad.nist.gov> <PRZEMEK.92Dec14171154@rrdstrad.nist.gov>
  12. NNTP-Posting-Host: eql.caltech.edu
  13. News-Software: VAX/VMS VNEWS 1.41    
  14.  
  15. [was Subject: Re: Why does this code crash with ACCVIO? (summary, solution)]
  16. In article <PRZEMEK.92Dec14171154@rrdstrad.nist.gov>,\
  17.  przemek@rrdstrad.nist.gov (Przemek Klosowski) writes...
  18. > Anyone cares to speculate why a separate keyword is
  19. > needed, rather than having "external" cause linking?
  20.  
  21.      There are several different models for external objects.  The one
  22. used by VAX C--and by GNU C for compatibility--is equivalent to Fortran
  23. COMMON blocks, doubtlessly for greater ease in multi-language applications.
  24. This is equivalent to using a named PSECT instead of a global symbol in
  25. Macro32; C's globaldef/globalref is vice versa.  If you had a Fortran
  26. BLOCK DATA module for data initialization you would have seen the same
  27. problem, except that the other declarations of the object in Fortran would
  28. have had to specify the proper size so the symptoms you'd see would have
  29. been different (zeroed data with your initialization missing, rather than
  30. ACCVIO due to wrong object size).
  31.  
  32.      This is usually only a problem with data-only object modules.  There
  33. are several ways around it.  Suppose FOO.C has just global variables in it.
  34. 1) explicitly link the data, either as a separate object file foo.obj or
  35.    as an explicit library reference.  link ...,mylib/libr/include=foo
  36.    Some people seem to be unaware of the latter variant.  xxx.olb/incl=yyy
  37.    is about the same as yyy.obj, but xxx.olb/lib/incl=yyy is more like
  38.    yyy.obj,xxx.olb/lib.
  39. 2) don't use data-only modules; always specify an initialization routine
  40.    even if it doesn't do anything.  Modify foo.c by adding
  41.    `void foo_init(void) { return; }' and add a call to `foo_init()'
  42.    in main() or some other appropriate place.  This is the method I prefer
  43.    because it's portable and the linker will complain if I don't supply
  44.    foo.obj (which can easily happen if relinking by hand instead of using
  45.    a make utility).
  46. 3) like #2, but use a globaldef variable and put a globalref reference
  47.    to it somewhere.  The somewhere could be in a header file used by all
  48.    the modules, so is more transparent than a dummy initialization routine,
  49.    but also non-portable.
  50. Both the second and third choices will allow the linker to pull module foo
  51. out of an object library without specifying the extra /include qualifier.
  52. With #3, be careful that GNU C's optimizer doesn't omit your unreferenced
  53. external(s) from the object module(s) though.
  54.  
  55.      DEC C for Alpha/VMS treats external objects differently by default,
  56. and has a qualifier called "/extern_model" for controlling it.  The options
  57. are "common_block", "relaxed_refdef", "strict_refdef", and "globalvalue".
  58. According to the online help, there's also a variant of strict_refdef
  59. which takes an extra name value, but I'm not sure offhand what that does.
  60. Relaxed_refdef is the default and requires that at least one definition
  61. exist (rather than the all `extern <type> <object>'; declarations such
  62. as you ended up with when the linker didn't bring in your data-only
  63. module).  Strict_refdef requires exactly one definition, which is what
  64. portable code should have anyway.
  65.  
  66.         Pat Rankin, rankin@eql.caltech.edu
  67.