home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12344 < prev    next >
Encoding:
Text File  |  1992-08-14  |  4.6 KB  |  126 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!mcsun!sunic!aun.uninett.no!nuug!nntp.uio.no!hbf
  3. From: hbf@durin.uio.no (Hallvard B Furuseth)
  4. Subject: Re: Macros for ANSI prototypes
  5. In-Reply-To: dave@cs.arizona.edu's message of 12 Aug 92 05:17:07 GMT
  6. Message-ID: <HBF.92Aug14220530@durin.uio.no>
  7. Sender: news@ulrik.uio.no (Mr News)
  8. Nntp-Posting-Host: durin.uio.no
  9. Organization: University of Oslo, Norway
  10. References: <1992Aug11.075855.13519@elroy.jpl.nasa.gov>
  11.     <1992Aug12.051707.17509@organpipe.uug.arizona.edu>
  12. Date: Fri, 14 Aug 1992 21:05:30 GMT
  13. Lines: 111
  14.  
  15. In article <1992Aug11.075855.13519@elroy.jpl.nasa.gov>, hyc@hanauma (Howard Chu) writes:
  16. >Well, since the topic has risen up again, here's a cheap way to write code
  17. >that works for either ANSI or K&R compilers...
  18. >##### cut here #####
  19. >/*
  20. > * ansi.h - macros for declaring functions and arguments with or without
  21. > * ANSI C prototypes. The __DECL macro is used to declare functions with
  22. > * prototypes. The __DEFN macro is used to actually define the functions.
  23.  
  24. Isn't the __ symbols reserved for the compiler?  What do you do when you
  25. stumble over a compiler which uses __DEFN for some assemby nonsense?
  26.  
  27. Here is another version.  I'm sure we are a lot of people who have been
  28. reinventing the wheel, is there more commonly used wheel out there?
  29.  
  30.  
  31. In article <1992Aug12.051707.17509@organpipe.uug.arizona.edu> dave@cs.arizona.edu (Dave Schaumann) writes:
  32. > In stead of obfuscating your code with macros that turn on and off
  33. > functio prototypes as the particular compiler requires, wouldn't it be
  34. > better to simply include one of the tools freely available to
  35. > automatically massage prototyped code into K&R acceptability.
  36.  
  37. And while working on this source tree whith both C++ and traditional C
  38. files I'd need a different include-directory to build the C headers in,
  39. 'make' must re-install there after every change to the C++ headers, and
  40. it gets worse if I want to test a change in one package before
  41. installing the .h files.
  42. I prefer an ansidef.h file.  Or better cooperating compilers, of course.
  43.  
  44.  
  45. /* ansidef.h -- compatibility macros for C++ / ANSI C / traditional C
  46.  *
  47.  * Usage examples:
  48.  *   IF_CPLUSPLUS(extern "C" {, EMPTY_ARG) / * anti-#if fanatics * /
  49.  *   extern int printf ARGDCL((CONST char *, ...));
  50.  *   extern VOIDFN foo ARGDCL((VOIDDATA *, int)), bar (NOARGS);
  51.  *   IF_CPLUSPLUS(}, EMPTY_ARG)
  52.  *   VOIDFN foo ARGDEF((a,b), VOIDDATA *a ARG int b) { bar(); return VOIDVAL; }
  53.  *   #if IF_ANSI(1,0)        / * Safer than ie __STDC__ || __cplusplus * /
  54.  *                / * because it matches what ansidef does  * /
  55.  *   #  include <stdarg.h>
  56.  *   #else
  57.  *   #  include <varargs.h>    / * Sorry, varargs is not really portable * /
  58.  *   #endif
  59.  *   int xprintf VA_VARGDEF((fmt, va_alist), CONST char *fmt)
  60.  *    {...VA_START(ap,fmt)...}
  61.  *
  62.  * Please add to this file to rope in more machine types and compilers.
  63.  * Hallvard B Furuseth <hbf@usit.uio.no>, Mar 1992
  64.  */
  65.  
  66. #ifndef ANSIDEF_H_
  67. #define ANSIDEF_H_
  68.  
  69. /* Give this to preprocessors that do not grok empty macro args */
  70. #define EMPTY_ARG
  71.  
  72. #ifdef __cplusplus
  73. #  define IF_CPLUSPLUS(cplpl,c)    cplpl
  74. #  define INLINE        inline    /* *ATTEMPT* to make inline function */
  75. #else
  76. #  define IF_CPLUSPLUS(cplpl,c)    c
  77. #if __GNUC__
  78. #  define INLINE        static __inline__
  79. #else
  80. #  define INLINE        static    /* Better than nothing */
  81. #endif
  82. #endif /* C++ */
  83.  
  84. #if defined(__STDC__) || defined(__cplusplus)
  85. #  define IF_ANSI(ansi,trad)    ansi
  86. #  define ARGDCL(args)        args    /* Arguments in function declaration */
  87. #  define ARGDEF(args,defs)    (defs)    /* Arguments in function definition  */
  88. #  define ARG            ,    /* Argument separator in definitions */
  89. #  define NOARGS        void    /* Arg to function with no arguments */
  90. #  define VA_ARGDEF(args,defs)    (defs, ...) /* Args to variable arguments fn */
  91. #  define VA_START        va_start /* Use this instead of va_start */
  92. #  define CONST            const    /* Qualifiers - need a type as well  */
  93. #  define SIGNED        signed
  94. #  define VOLATILE        volatile
  95. #  define HAS_VOID        1
  96. #else
  97. #  define IF_ANSI(ansi,trad)    trad
  98. #  define ARGDCL(args)        ()
  99. #  define ARGDEF(args,defs)    args defs;
  100. #  define ARG            ;
  101. #  define NOARGS
  102. #  define VA_ARGDEF(args,defs)    args defs; va_dcl
  103. #  define VA_START(ptr,last)    va_start((ptr))
  104. #  define CONST
  105. #  define SIGNED
  106. #  define VOLATILE
  107. #endif /* ANSI/C++ */
  108.  
  109. #if HAS_VOID ||\
  110.     defined(ultrix) || defined(sun) ||\
  111.     defined(__ultrix__) || defined(__sun__) /* gcc versions */
  112. #  define HAS_VOID        1    /* True if 'void' exists */
  113. #  define VOIDDATA        void    /* Data type of void* "contents"  */
  114. #  define VOIDFN        void    /* Return type for void functions */
  115. #  define VOIDVAL            /* Return value for void functions */
  116. #else
  117. #  define VOIDDATA        char
  118. #  define VOIDFN        int
  119. #  define VOIDVAL        0
  120. #endif /* HAS_VOID */
  121.  
  122. #endif /* ANSIDEF_H_ */
  123. --
  124.  
  125. Hallvard
  126.