home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18693 < prev    next >
Encoding:
Text File  |  1993-01-05  |  3.6 KB  |  79 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!lucid.com!lucid.com!jss
  3. From: jss@lucid.com (Jerry Schwarz)
  4. Subject: Re: Standard library functions and macros
  5. Message-ID: <1993Jan5.032555.12777@lucid.com>
  6. Sender: usenet@lucid.com
  7. Reply-To: jss@lucid.com (Jerry Schwarz)
  8. Organization: Lucid, Inc.
  9. References: <1992Dec29.213548.5661@microsoft.com> <9300302.725@mulga.cs.mu.OZ.AU> <1993Jan2.191336.28542@taumet.com> <1993Jan04.231519.8704@microsoft.com>
  10. Date: Tue, 5 Jan 93 03:25:55 GMT
  11. Lines: 66
  12.  
  13. In article <1993Jan04.231519.8704@microsoft.com>, jimad@microsoft.com (Jim Adcock) writes:
  14. |> In article <1993Jan2.191336.28542@taumet.com> steve@taumet.com (Steve Clamage) writes:
  15. |> |If an implementation uses the same library for C and C++, which is
  16. |> |true for current implemenations, you can successfully do this:
  17. |> |    extern "C" int puts(const char *);
  18. |> |    int main() { puts("Hello world\n"); return 0; }
  19. |> |You are just not assured the code will work.
  20. |> 
  21. |> How about implementations and programmers that want to continue to
  22. |> share code between C and C++ implementations?  Let's say a program
  23. |> that it created from some "C" files and some "C++" files, and each of
  24. |> these files in turn calls some of the standard-C functions.  How are
  25. |> you going to make this work?  Seems to me you are requiring 
  26. |> "C++ calling convention" of the standard-C functions called by the
  27. |> C++ files, whereas the standard-C functions called from the "C" files
  28. |> would have to follow "C" calling convention.  At the very least this
  29. |> seems to me to be saying that you're requiring dual-entry stubs of
  30. |> all the library functions.  And then how do these things coexist in
  31. |> one library and get linked in correctly?  Can you point to even one
  32. |> existing implementation that implements your proposed changes, and
  33. |> which continues to allow mixed Cfile/C++file programs to be compiled and linked?
  34.  
  35. I really don't understand Jim's concerns. All the vendors (of
  36. Unix compilers) I've looked at take a similar approach to this problem.
  37. As far as I can tell we would have no problem accomodating the currently
  38. proposed X3J16 requirements.
  39.  
  40. I'll describe our approach in more detail. (I usually avoid
  41. specific discussion of Lucid's compiler in this group, but
  42. it seems appropriate here.)
  43.  
  44. Lucid supplies a large collection of header files with its
  45. compiler that allow use of the vendor (Sun) supplied C libraries. 
  46. We share these headers between C++ and C without much problem.  
  47. These headers check for #ifdef __cplusplus and "do the right thing".
  48. When compiling C++ they insert extern "C" as appropriate.
  49. In C++ modes we have also found it useful to define variants
  50. of some of the C functions using the static inline technique
  51. discussed previously by Steve.
  52.  
  53. For example, in C++ modes our headers contain (after expansion of
  54. some macros that allow us to control variation between compilation modes)
  55.  
  56.     typedef void (*__sigtype)(int);
  57.     typedef void (*__alt_sigtype)(int,int,struct sigcontext*,char*);
  58.  
  59.     /* The C standard declaration */
  60.     extern "C" __sigtype signal(int,__sigtype) ;
  61.  
  62.     /* A variant expected by Sun programmers */
  63.     extern "C++" inline __alt_sigtype signal(int __sig,  __alt_sigtype __h) {
  64.         return (__alt_sigtype)signal( __sig, (__sigtype)__h) ;
  65.     }
  66.  
  67. The extern "C" variant gets linked directly to Sun's C library
  68. without any problems. We don't need stubs.
  69.  
  70. The variant was required because that is, in fact, the type of signal
  71. according to the Sun man pages
  72.  
  73. When compiling in strict ANSI C mode, the header just expands to
  74.  
  75.     typedef void (*__sigtype)(int);
  76.     __sigtype signal(int,__sigtype);
  77.  
  78.     -- Jerry Schwarz
  79.