Incompatibilities.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Incompatibilities</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Incompatibilities">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Incompatibilities">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Trouble.html#Trouble" rel="up" title="Trouble">
  30. <link href="Fixed-Headers.html#Fixed-Headers" rel="next" title="Fixed Headers">
  31. <link href="Interoperation.html#Interoperation" rel="prev" title="Interoperation">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Incompatibilities"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Fixed-Headers.html#Fixed-Headers" accesskey="n" rel="next">Fixed Headers</a>, Previous: <a href="Interoperation.html#Interoperation" accesskey="p" rel="prev">Interoperation</a>, Up: <a href="Trouble.html#Trouble" accesskey="u" rel="up">Trouble</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Incompatibilities-of-GCC"></a>
  69. <h3 class="section">13.3 Incompatibilities of GCC</h3>
  70. <a name="index-incompatibilities-of-GCC"></a>
  71. <a name="index-traditional-1"></a>
  72. <p>There are several noteworthy incompatibilities between GNU C and K&amp;R
  73. (non-ISO) versions of C.
  74. </p>
  75. <ul>
  76. <li> <a name="index-string-constants"></a>
  77. <a name="index-read_002donly-strings"></a>
  78. <a name="index-shared-strings"></a>
  79. GCC normally makes string constants read-only. If several
  80. identical-looking string constants are used, GCC stores only one
  81. copy of the string.
  82. <a name="index-mktemp_002c-and-constant-strings"></a>
  83. <p>One consequence is that you cannot call <code>mktemp</code> with a string
  84. constant argument. The function <code>mktemp</code> always alters the
  85. string its argument points to.
  86. </p>
  87. <a name="index-sscanf_002c-and-constant-strings"></a>
  88. <a name="index-fscanf_002c-and-constant-strings"></a>
  89. <a name="index-scanf_002c-and-constant-strings"></a>
  90. <p>Another consequence is that <code>sscanf</code> does not work on some very
  91. old systems when passed a string constant as its format control string
  92. or input. This is because <code>sscanf</code> incorrectly tries to write
  93. into the string constant. Likewise <code>fscanf</code> and <code>scanf</code>.
  94. </p>
  95. <p>The solution to these problems is to change the program to use
  96. <code>char</code>-array variables with initialization strings for these
  97. purposes instead of string constants.
  98. </p>
  99. </li><li> <code>-2147483648</code> is positive.
  100. <p>This is because 2147483648 cannot fit in the type <code>int</code>, so
  101. (following the ISO C rules) its data type is <code>unsigned long int</code>.
  102. Negating this value yields 2147483648 again.
  103. </p>
  104. </li><li> GCC does not substitute macro arguments when they appear inside of
  105. string constants. For example, the following macro in GCC
  106. <div class="smallexample">
  107. <pre class="smallexample">#define foo(a) &quot;a&quot;
  108. </pre></div>
  109. <p>will produce output <code>&quot;a&quot;</code> regardless of what the argument <var>a</var> is.
  110. </p>
  111. </li><li> <a name="index-setjmp-incompatibilities"></a>
  112. <a name="index-longjmp-incompatibilities"></a>
  113. When you use <code>setjmp</code> and <code>longjmp</code>, the only automatic
  114. variables guaranteed to remain valid are those declared
  115. <code>volatile</code>. This is a consequence of automatic register
  116. allocation. Consider this function:
  117. <div class="smallexample">
  118. <pre class="smallexample">jmp_buf j;
  119. foo ()
  120. {
  121. int a, b;
  122. a = fun1 ();
  123. if (setjmp (j))
  124. return a;
  125. a = fun2 ();
  126. /* <span class="roman"><code>longjmp (j)</code> may occur in <code>fun3</code>.</span> */
  127. return a + fun3 ();
  128. }
  129. </pre></div>
  130. <p>Here <code>a</code> may or may not be restored to its first value when the
  131. <code>longjmp</code> occurs. If <code>a</code> is allocated in a register, then
  132. its first value is restored; otherwise, it keeps the last value stored
  133. in it.
  134. </p>
  135. <a name="index-W-3"></a>
  136. <p>If you use the <samp>-W</samp> option with the <samp>-O</samp> option, you will
  137. get a warning when GCC thinks such a problem might be possible.
  138. </p>
  139. </li><li> Programs that use preprocessing directives in the middle of macro
  140. arguments do not work with GCC. For example, a program like this
  141. will not work:
  142. <div class="smallexample">
  143. <pre class="smallexample">foobar (
  144. #define luser
  145. hack)
  146. </pre></div>
  147. <p>ISO C does not permit such a construct.
  148. </p>
  149. </li><li> K&amp;R compilers allow comments to cross over an inclusion boundary
  150. (i.e. started in an include file and ended in the including file).
  151. </li><li> <a name="index-external-declaration-scope"></a>
  152. <a name="index-scope-of-external-declarations"></a>
  153. <a name="index-declaration-scope"></a>
  154. Declarations of external variables and functions within a block apply
  155. only to the block containing the declaration. In other words, they
  156. have the same scope as any other declaration in the same place.
  157. <p>In some other C compilers, an <code>extern</code> declaration affects all the
  158. rest of the file even if it happens within a block.
  159. </p>
  160. </li><li> In traditional C, you can combine <code>long</code>, etc., with a typedef name,
  161. as shown here:
  162. <div class="smallexample">
  163. <pre class="smallexample">typedef int foo;
  164. typedef long foo bar;
  165. </pre></div>
  166. <p>In ISO C, this is not allowed: <code>long</code> and other type modifiers
  167. require an explicit <code>int</code>.
  168. </p>
  169. </li><li> <a name="index-typedef-names-as-function-parameters"></a>
  170. PCC allows typedef names to be used as function parameters.
  171. </li><li> Traditional C allows the following erroneous pair of declarations to
  172. appear together in a given scope:
  173. <div class="smallexample">
  174. <pre class="smallexample">typedef int foo;
  175. typedef foo foo;
  176. </pre></div>
  177. </li><li> GCC treats all characters of identifiers as significant. According to
  178. K&amp;R-1 (2.2), &ldquo;No more than the first eight characters are significant,
  179. although more may be used.&rdquo;. Also according to K&amp;R-1 (2.2), &ldquo;An
  180. identifier is a sequence of letters and digits; the first character must
  181. be a letter. The underscore _ counts as a letter.&rdquo;, but GCC also
  182. allows dollar signs in identifiers.
  183. </li><li> <a name="index-whitespace"></a>
  184. PCC allows whitespace in the middle of compound assignment operators
  185. such as &lsquo;<samp>+=</samp>&rsquo;. GCC, following the ISO standard, does not
  186. allow this.
  187. </li><li> <a name="index-apostrophes"></a>
  188. <a name="index-_0027"></a>
  189. GCC complains about unterminated character constants inside of
  190. preprocessing conditionals that fail. Some programs have English
  191. comments enclosed in conditionals that are guaranteed to fail; if these
  192. comments contain apostrophes, GCC will probably report an error. For
  193. example, this code would produce an error:
  194. <div class="smallexample">
  195. <pre class="smallexample">#if 0
  196. You can't expect this to work.
  197. #endif
  198. </pre></div>
  199. <p>The best solution to such a problem is to put the text into an actual
  200. C comment delimited by &lsquo;<samp>/*&hellip;*/</samp>&rsquo;.
  201. </p>
  202. </li><li> Many user programs contain the declaration &lsquo;<samp>long time ();</samp>&rsquo;. In the
  203. past, the system header files on many systems did not actually declare
  204. <code>time</code>, so it did not matter what type your program declared it to
  205. return. But in systems with ISO C headers, <code>time</code> is declared to
  206. return <code>time_t</code>, and if that is not the same as <code>long</code>, then
  207. &lsquo;<samp>long time ();</samp>&rsquo; is erroneous.
  208. <p>The solution is to change your program to use appropriate system headers
  209. (<code>&lt;time.h&gt;</code> on systems with ISO C headers) and not to declare
  210. <code>time</code> if the system header files declare it, or failing that to
  211. use <code>time_t</code> as the return type of <code>time</code>.
  212. </p>
  213. </li><li> <a name="index-float-as-function-value-type"></a>
  214. When compiling functions that return <code>float</code>, PCC converts it to
  215. a double. GCC actually returns a <code>float</code>. If you are concerned
  216. with PCC compatibility, you should declare your functions to return
  217. <code>double</code>; you might as well say what you mean.
  218. </li><li> <a name="index-structures"></a>
  219. <a name="index-unions"></a>
  220. When compiling functions that return structures or unions, GCC
  221. output code normally uses a method different from that used on most
  222. versions of Unix. As a result, code compiled with GCC cannot call
  223. a structure-returning function compiled with PCC, and vice versa.
  224. <p>The method used by GCC is as follows: a structure or union which is
  225. 1, 2, 4 or 8 bytes long is returned like a scalar. A structure or union
  226. with any other size is stored into an address supplied by the caller
  227. (usually in a special, fixed register, but on some machines it is passed
  228. on the stack). The target hook <code>TARGET_STRUCT_VALUE_RTX</code>
  229. tells GCC where to pass this address.
  230. </p>
  231. <p>By contrast, PCC on most target machines returns structures and unions
  232. of any size by copying the data into an area of static storage, and then
  233. returning the address of that storage as if it were a pointer value.
  234. The caller must copy the data from that memory area to the place where
  235. the value is wanted. GCC does not use this method because it is
  236. slower and nonreentrant.
  237. </p>
  238. <p>On some newer machines, PCC uses a reentrant convention for all
  239. structure and union returning. GCC on most of these machines uses a
  240. compatible convention when returning structures and unions in memory,
  241. but still returns small structures and unions in registers.
  242. </p>
  243. <a name="index-fpcc_002dstruct_002dreturn-1"></a>
  244. <p>You can tell GCC to use a compatible convention for all structure and
  245. union returning with the option <samp>-fpcc-struct-return</samp>.
  246. </p>
  247. </li><li> <a name="index-preprocessing-tokens"></a>
  248. <a name="index-preprocessing-numbers"></a>
  249. GCC complains about program fragments such as &lsquo;<samp>0x74ae-0x4000</samp>&rsquo;
  250. which appear to be two hexadecimal constants separated by the minus
  251. operator. Actually, this string is a single <em>preprocessing token</em>.
  252. Each such token must correspond to one token in C. Since this does not,
  253. GCC prints an error message. Although it may appear obvious that what
  254. is meant is an operator and two values, the ISO C standard specifically
  255. requires that this be treated as erroneous.
  256. <p>A <em>preprocessing token</em> is a <em>preprocessing number</em> if it
  257. begins with a digit and is followed by letters, underscores, digits,
  258. periods and &lsquo;<samp>e+</samp>&rsquo;, &lsquo;<samp>e-</samp>&rsquo;, &lsquo;<samp>E+</samp>&rsquo;, &lsquo;<samp>E-</samp>&rsquo;, &lsquo;<samp>p+</samp>&rsquo;,
  259. &lsquo;<samp>p-</samp>&rsquo;, &lsquo;<samp>P+</samp>&rsquo;, or &lsquo;<samp>P-</samp>&rsquo; character sequences. (In strict C90
  260. mode, the sequences &lsquo;<samp>p+</samp>&rsquo;, &lsquo;<samp>p-</samp>&rsquo;, &lsquo;<samp>P+</samp>&rsquo; and &lsquo;<samp>P-</samp>&rsquo; cannot
  261. appear in preprocessing numbers.)
  262. </p>
  263. <p>To make the above program fragment valid, place whitespace in front of
  264. the minus sign. This whitespace will end the preprocessing number.
  265. </p></li></ul>
  266. <hr>
  267. <div class="header">
  268. <p>
  269. Next: <a href="Fixed-Headers.html#Fixed-Headers" accesskey="n" rel="next">Fixed Headers</a>, Previous: <a href="Interoperation.html#Interoperation" accesskey="p" rel="prev">Interoperation</a>, Up: <a href="Trouble.html#Trouble" accesskey="u" rel="up">Trouble</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  270. </div>
  271. </body>
  272. </html>