bugs.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <html>
  2. <head>
  3. <title>GCC Bugs</title>
  4. </head>
  5. <body>
  6. <h1>GCC Bugs</h1>
  7. <p>The latest version of this document is always available at
  8. <a href="http://gcc.gnu.org/bugs.html">http://gcc.gnu.org/bugs.html</a>.</p>
  9. <hr />
  10. <h2>Table of Contents</h2>
  11. <ul>
  12. <li><a href="#report">Reporting Bugs</a>
  13. <ul>
  14. <li><a href="#need">What we need</a></li>
  15. <li><a href="#dontwant">What we DON'T want</a></li>
  16. <li><a href="#where">Where to post it</a></li>
  17. <li><a href="#detailed">Detailed bug reporting instructions</a></li>
  18. <li><a href="#gnat">Detailed bug reporting instructions for GNAT</a></li>
  19. <li><a href="#pch">Detailed bug reporting instructions when using a precompiled header</a></li>
  20. </ul>
  21. </li>
  22. <li><a href="#known">Frequently Reported Bugs in GCC</a>
  23. <ul>
  24. <li><a href="#cxx">C++</a>
  25. <ul>
  26. <li><a href="#cxx-abi">ABI bugs</a></li>
  27. <li><a href="#missing">Missing features</a></li>
  28. <li><a href="#parsing">Parse errors for "simple" code</a></li>
  29. </ul>
  30. </li>
  31. <li><a href="#fortran">Fortran</a></li>
  32. </ul>
  33. </li>
  34. <li><a href="#nonbugs">Non-bugs</a>
  35. <ul>
  36. <li><a href="#nonbugs_general">General</a></li>
  37. <li><a href="#nonbugs_c">C</a></li>
  38. <li><a href="#nonbugs_cxx">C++</a>
  39. <ul>
  40. <li><a href="#updating">Common problems updating from G++ 2.95 to
  41. G++ 3.0</a></li>
  42. </ul>
  43. </li>
  44. </ul>
  45. </li>
  46. </ul>
  47. <hr />
  48. <h1><a name="report">Reporting Bugs</a></h1>
  49. <p>The main purpose of a bug report is to enable us to fix the bug. The
  50. most important prerequisite for this is that the report must be complete and
  51. self-contained, which we explain in detail below.</p>
  52. <p>Before you report a bug, please check the
  53. <a href="#known">list of well-known bugs</a> and, <strong>if possible
  54. in any way, try a current development snapshot</strong>.
  55. If you want to report a bug with versions of GCC before 3.1 we strongly
  56. recommend upgrading to the current release first.</p>
  57. <p>Before reporting that GCC compiles your code incorrectly, please
  58. compile it with <code>gcc -Wall</code> and see whether this shows
  59. anything wrong with your code that could be the cause instead of a bug
  60. in GCC.</p>
  61. <h2>Summarized bug reporting instructions</h2>
  62. <p>After this summary, you'll find detailed bug reporting
  63. instructions, that explain how to obtain some of the information
  64. requested in this summary.</p>
  65. <h3><a name="need">What we need</a></h3>
  66. <p>Please include in your bug report all of the following items, the first
  67. three of which can be obtained from the output of <code>gcc -v</code>:</p>
  68. <ul>
  69. <li>the exact version of GCC;</li>
  70. <li>the system type;</li>
  71. <li>the options given when GCC was configured/built;</li>
  72. <li>the complete command line that triggers the bug;</li>
  73. <li>the compiler output (error messages, warnings, etc.); and</li>
  74. <li>the <em>preprocessed</em> file (<code>*.i*</code>) that triggers the
  75. bug, generated by adding <code>-save-temps</code> to the complete
  76. compilation command, or, in the case of a bug report for the GNAT front end,
  77. a complete set of source files (see below).</li>
  78. </ul>
  79. <h3><a name="dontwant">What we do <strong>not</strong> want</a></h3>
  80. <ul>
  81. <li>A source file that <code>#include</code>s header files that are left
  82. out of the bug report (see above)</li>
  83. <li>That source file and a collection of header files.</li>
  84. <li>An attached archive (tar, zip, shar, whatever) containing all
  85. (or some :-) of the above.</li>
  86. <li>A code snippet that won't cause the compiler to produce the
  87. exact output mentioned in the bug report (e.g., a snippet with just
  88. a few lines around the one that <b>apparently</b> triggers the bug,
  89. with some pieces replaced with ellipses or comments for extra
  90. obfuscation :-)</li>
  91. <li>The location (URL) of the package that failed to build (we won't
  92. download it, anyway, since you've already given us what we need to
  93. duplicate the bug, haven't you? :-)</li>
  94. <li>An error that occurs only some of the times a certain file is
  95. compiled, such that retrying a sufficient number of times results in
  96. a successful compilation; this is a symptom of a hardware problem,
  97. not of a compiler bug (sorry)</li>
  98. <li>E-mail messages that complement previous, incomplete bug
  99. reports. Post a new, self-contained, full bug report instead, if
  100. possible as a follow-up to the original bug report</li>
  101. <li>Assembly files (<code>*.s</code>) produced by the compiler, or any
  102. binary files, such as object files, executables, core files, or
  103. precompiled header files</li>
  104. <li>Duplicate bug reports, or reports of bugs already fixed in the
  105. development tree, especially those that have already been reported
  106. as fixed last week :-)</li>
  107. <li>Bugs in the assembler, the linker or the C library. These are
  108. separate projects, with separate mailing lists and different bug
  109. reporting procedures</li>
  110. <li>Bugs in releases or snapshots of GCC not issued by the GNU
  111. Project. Report them to whoever provided you with the release</li>
  112. <li>Questions about the correctness or the expected behavior of
  113. certain constructs that are not GCC extensions. Ask them in forums
  114. dedicated to the discussion of the programming language</li>
  115. </ul>
  116. <h3><a name="where">Where to post it</a></h3>
  117. <p>Please submit your bug report directly to the
  118. <a href="http://gcc.gnu.org/bugzilla/">GCC bug database</a>.
  119. Alternatively, you can use the <code>gccbug</code> script that mails your bug
  120. report to the bug database.
  121. Only if all this is absolutely impossible, mail all information to
  122. <a href="mailto:gcc-bugs@gcc.gnu.org">gcc-bugs@gcc.gnu.org</a>, but note that
  123. such reports are often overlooked since they are not permanently recorded into
  124. the database for later processing.</p>
  125. <h2><a name="detailed">Detailed bug reporting instructions</a></h2>
  126. <p>Please refer to the <a href="#gnat">next section</a> when reporting
  127. bugs in GNAT, the Ada compiler, or to the <a href="#pch">one after
  128. that</a> when reporting bugs that appear when using a precompiled header.</p>
  129. <p>In general, all the information we need can be obtained by
  130. collecting the command line below, as well as its output and the
  131. preprocessed file it generates.</p>
  132. <blockquote><p><code>gcc -v -save-temps <i>all-your-options
  133. source-file</i></code></p></blockquote>
  134. <p>Typically the preprocessed file (extension <code>.i</code> for C or
  135. <code>.ii</code> for C++, and <code>.f</code> if the preprocessor is used on
  136. Fortran files) will be large, so please compress the
  137. resulting file with one of the popular compression programs such as
  138. bzip2, gzip, zip or compress (in
  139. decreasing order of preference). Use maximum compression
  140. (<code>-9</code>) if available. Please include the compressed
  141. preprocessor output in your bug report, even if the source code is
  142. freely available elsewhere; it makes the job of our volunteer testers
  143. much easier.</p>
  144. <p>The <b>only</b> excuses to not send us the preprocessed sources are
  145. (i) if you've found a bug in the preprocessor, (ii) if you've reduced
  146. the testcase to a small file that doesn't include any other file or
  147. (iii) if the bug appears only when using precompiled headers. If you
  148. can't post the preprocessed sources because they're proprietary code,
  149. then try to create a small file that triggers the same problem.</p>
  150. <p>Since we're supposed to be able to re-create the assembly output
  151. (extension <code>.s</code>), you usually should not include
  152. it in the bug report, although you may want to post parts of it to
  153. point out assembly code you consider to be wrong.</p>
  154. <p>Whether to use MIME attachments or <code>uuencode</code> is up to
  155. you. In any case, make sure the compiler command line, version and
  156. error output are in plain text, so that we don't have to decode the
  157. bug report in order to tell who should take care of it. A meaningful
  158. subject indicating language and platform also helps.</p>
  159. <p>Please avoid posting an archive (.tar, .shar or .zip); we generally
  160. need just a single file to reproduce the bug (the .i/.ii/.f preprocessed
  161. file), and, by storing it in an archive, you're just making our
  162. volunteers' jobs harder. Only when your bug report requires multiple
  163. source files to be reproduced should you use an archive. This is, for example,
  164. the case if you are using <code>INCLUDE</code> directives in Fortran code,
  165. which are not processed by the preprocessor, but the compiler. In that case,
  166. we need the main file and all <code>INCLUDE</code>d files. In any case,
  167. make sure the compiler version, error message, etc, are included in
  168. the body of your bug report as plain text, even if needlessly
  169. duplicated as part of an archive.</p>
  170. <p>If you fail to supply enough information for a bug report to be
  171. reproduced, someone will probably ask you to post additional
  172. information (or just ignore your bug report, if they're in a bad day,
  173. so try to get it right on the first posting :-). In this case, please
  174. post the additional information to the bug reporting mailing list, not
  175. just to the person who requested it, unless explicitly told so. If
  176. possible, please include in this follow-up all the information you had
  177. supplied in the incomplete bug report (including the preprocessor
  178. output), so that the new bug report is self-contained.</p>
  179. <h2><a name="gnat">Detailed bug reporting instructions for GNAT</a></h2>
  180. <p>See the <a href="#detailed">previous section</a> for bug reporting
  181. instructions for GCC language implementations other than Ada.</p>
  182. <p>Bug reports have to contain at least the following information in
  183. order to be useful:</p>
  184. <ul>
  185. <li>the exact version of GCC, as shown by "<code>gcc -v</code>";</li>
  186. <li>the system type;</li>
  187. <li>the options when GCC was configured/built;</li>
  188. <li>the exact command line passed to the <code>gcc</code> program
  189. triggering the bug
  190. (not just the flags passed to <code>gnatmake</code>, but
  191. <code>gnatmake</code> prints the parameters it passed to <code>gcc</code>)</li>
  192. <li>a collection of source files for reproducing the bug,
  193. preferably a minimal set (see below);</li>
  194. <li>a description of the expected behavior;</li>
  195. <li>a description of actual behavior.</li>
  196. </ul>
  197. <p>If your code depends on additional source files (usually package
  198. specifications), submit the source code for these compilation units in
  199. a single file that is acceptable input to <code>gnatchop</code>,
  200. i.e. contains no non-Ada text. If the compilation terminated
  201. normally, you can usually obtain a list of dependencies using the
  202. "<code>gnatls -d <i>main_unit</i></code>" command, where
  203. <code><i>main_unit</i></code> is the file name of the main compilation
  204. unit (which is also passed to <code>gcc</code>).</p>
  205. <p>If you report a bug which causes the compiler to print a bug box,
  206. include that bug box in your report, and do not forget to send all the
  207. source files listed after the bug box along with your report.</p>
  208. <p>If you use <code>gnatprep</code>, be sure to send in preprocessed
  209. sources (unless you have to report a bug in <code>gnatprep</code>).</p>
  210. <p>When you have checked that your report meets these criteria, please
  211. submit it according to our <a href="#where">generic instructions</a>.
  212. (If you use a mailing list for reporting, please include an
  213. "<code>[Ada]</code>" tag in the subject.)</p>
  214. <h2><a name="pch">Detailed bug reporting instructions when using a
  215. precompiled header</a></h2>
  216. <p>If you're encountering a bug when using a precompiled header, the
  217. first thing to do is to delete the precompiled header, and try running
  218. the same GCC command again. If the bug happens again, the bug doesn't
  219. really involve precompiled headers, please report it without using
  220. them by following the instructions <a href="#detailed">above</a>.</p>
  221. <p>If you've found a bug while <i>building</i> a precompiled header
  222. (for instance, the compiler crashes), follow the usual instructions
  223. <a href="#detailed">above</a>.</p>
  224. <p>If you've found a real precompiled header bug, what we'll need to
  225. reproduce it is the sources to build the precompiled header (as a
  226. single <code>.i</code> file), the source file that uses the
  227. precompiled header, any other headers that source file includes, and
  228. the command lines that you used to build the precompiled header and to
  229. use it.</p>
  230. <p>Please <strong>don't</strong> send us the actual precompiled
  231. header. It is likely to be very large and we can't use it to
  232. reproduce the problem.</p>
  233. <hr />
  234. <h1><a name="known">Frequently Reported Bugs in GCC</a></h1>
  235. <p>This is a list of bugs in GCC that are reported very often, but not
  236. yet fixed. While it is certainly better to fix bugs instead of documenting
  237. them, this document might save people the effort of writing a bug report
  238. when the bug is already well-known.</p>
  239. <p>There are many reasons why a reported bug doesn't get fixed.
  240. It might be difficult to fix, or fixing it might break compatibility.
  241. Often, reports get a low priority when there is a simple work-around.
  242. In particular, bugs caused by invalid code have a simple work-around:
  243. <em>fix the code</em>.</p>
  244. <hr />
  245. <h2><a name="cxx">C++</a></h2>
  246. <h3><a name="cxx-abi">ABI bugs</a></h3>
  247. <p>GCC 3.0 had a new ABI, which affected class layout, function mangling and
  248. calling conventions. We had intended it to be complete, but unfortunately
  249. some issues came to light, too late to fix in the 3.0 series.
  250. The ABI should not change in dot releases, so we addressed most issues
  251. in GCC 3.1.</p>
  252. <dl>
  253. <dt>Covariant return types</dt>
  254. <dd>Up to (and including) GCC 3.3 we did not implement non-trivial
  255. covariant returns. This has been addressed for GCC 3.4.</dd>
  256. </dl>
  257. <h3><a name="missing">Missing features</a></h3>
  258. <p>We know some things are missing from G++.</p>
  259. <dl>
  260. <dt>The <code>export</code> keyword is not implemented.</dt>
  261. <dd><p>Most C++ compilers (G++ included) do not yet implement
  262. <code>export</code>, which is necessary for separate compilation of
  263. template declarations and definitions. Without <code>export</code>, a
  264. template definition must be in scope to be used. The obvious
  265. workaround is simply to place all definitions in the header
  266. itself. Alternatively, the compilation unit containing template
  267. definitions may be included from the header.</p></dd>
  268. <dt>Two stage lookup in templates is not implemented.</dt>
  269. <dd><p>[14.6] specifies how names are looked up inside a template. G++
  270. does not do this correctly, but for most templates this will not be
  271. noticeable.</p></dd>
  272. </dl>
  273. <h3><a name="parsing">Parse errors for "simple" code</a></h3>
  274. <p>Up to and including GCC 3.0, the compiler will give "parse error" for
  275. seemingly simple code, such as</p>
  276. <pre>
  277. struct A{
  278. A();
  279. A(int);
  280. void func();
  281. };
  282. struct B{
  283. B(A);
  284. B(A,A);
  285. void func();
  286. };
  287. void foo(){
  288. B b(A(),A(1)); //Variable b, initialized with two temporaries
  289. B(A(2)).func(); //B temporary, initialized with A temporary
  290. }
  291. </pre>
  292. <p>The problem is that GCC starts to parse the declaration of
  293. <code>b</code> as a function <code>b</code> returning <code>B</code>,
  294. taking a function returning <code>A</code> as an argument. When it
  295. sees the 1, it is too late. The work-around in these cases is to add
  296. additional parentheses around the expressions that are mistaken as
  297. declarations:</p>
  298. <pre>
  299. (B(A(2))).func();
  300. </pre>
  301. <p>Sometimes, even that is not enough; to show the compiler that this
  302. should be really an expression, a comma operator with a dummy argument
  303. can be used:</p>
  304. <pre>
  305. B b((0,A()),A(1));
  306. </pre>
  307. <p>Another example is the parse error for the <code>return</code>
  308. statement in</p>
  309. <pre>
  310. struct A{};
  311. struct B{
  312. A a;
  313. A f1(bool);
  314. };
  315. A B::f1(bool b)
  316. {
  317. if (b)
  318. return (A());
  319. return a;
  320. }
  321. </pre>
  322. <p>The problem is that the compiler interprets <code>A()</code> as a
  323. function (taking no arguments, returning <code>A</code>), and
  324. <code>(A()</code>) as a cast - with a missing expression, hence the
  325. parse error. The work-around is to omit the parentheses:</p>
  326. <pre>
  327. if (b)
  328. return A();
  329. </pre>
  330. <p>This problem occurs in a number of variants; in <code>throw</code>
  331. statements, people also frequently put the object in parentheses. The
  332. exact error also somewhat varies with the compiler version. The
  333. work-arounds proposed do not change the semantics of the program at
  334. all; they make them perhaps less readable.</p>
  335. <hr />
  336. <h2><a name="fortran">Fortran</a></h2>
  337. <p>Fortran bugs are documented in the G77 manual rather than
  338. explicitly listed here. Please see
  339. <a href="http://gcc.gnu.org/onlinedocs/g77/Trouble.html">Known Causes of
  340. Trouble with GNU Fortran</a> in the G77 manual.</p>
  341. <hr />
  342. <h1><a name="nonbugs">Non-bugs</a></h1>
  343. <p>The following are not actually bugs, but are reported often
  344. enough to warrant a mention here.</p>
  345. <p>It is not always a bug in the compiler, if code which "worked" in a
  346. previous version, is now rejected. Earlier versions of GCC sometimes were
  347. less picky about standard conformance and accepted invalid source code.
  348. In addition, programming languages themselves change, rendering code
  349. invalid that used to be conforming (this holds especially for C++).
  350. In either case, you should update your code to match recent language
  351. standards.</p>
  352. <hr />
  353. <h2><a name="nonbugs_general">General</a></h2>
  354. <dl>
  355. <dt>Problems with floating point numbers - the
  356. <a href="http://gcc.gnu.org/PR323">most often reported non-bug</a>.</dt>
  357. <dd><p>In a number of cases, GCC appears to perform floating point
  358. computations incorrectly. For example, the C++ program</p>
  359. <blockquote><pre>
  360. #include &lt;iostream&gt;
  361. int main()
  362. {
  363. double a = 0.5;
  364. double b = 0.01;
  365. std::cout &lt;&lt; (int)(a / b) &lt;&lt; std::endl;
  366. return 0;
  367. }
  368. </pre></blockquote>
  369. <p>might print 50 on some systems and optimization levels, and 49 on
  370. others.</p>
  371. <p>The is the result of <em>rounding</em>: The computer cannot
  372. represent all real numbers exactly, so it has to use
  373. approximations. When computing with approximation, the computer needs
  374. to round to the nearest representable number.</p>
  375. <p>This is not a bug in the compiler, but an inherent limitation of
  376. the floating point types. Please study
  377. <a href="http://www.validlab.com/goldberg/paper.ps">this paper</a>
  378. for more information.</p></dd>
  379. </dl>
  380. <hr />
  381. <h2><a name="nonbugs_c">C</a></h2>
  382. <dl>
  383. <dt>Casting does not work as expected when optimization is turned on.</dt>
  384. <dd><p>This is often caused by a violation of aliasing rules, which are part
  385. of the ISO C standard. These rules say that a program is invalid if you try
  386. to access a variable through a pointer of an incompatible type. This is
  387. happening in the following example where a short is accessed through a
  388. pointer to integer (the code assumes 16-bit <code>short</code>s and 32-bit
  389. <code>int</code>s):</p>
  390. <blockquote><pre>
  391. #include &lt;stdio.h&gt;
  392. int main()
  393. {
  394. short a[2];
  395. a[0]=0x1111;
  396. a[1]=0x1111;
  397. *(int *)a = 0x22222222; /* violation of aliasing rules */
  398. printf("%x %x\n", a[0], a[1]);
  399. return 0;
  400. }
  401. </pre></blockquote>
  402. <p>The aliasing rules were designed to allow compilers more aggressive
  403. optimization. Basically, a compiler can assume that all changes to variables
  404. happen through pointers or references to variables of a type compatible to
  405. the accessed variable. Dereferencing a pointer that violates the aliasing
  406. rules results in undefined behavior.</p>
  407. <p>In the case above, the compiler may assume that no access through an
  408. integer pointer can change the array <code>a</code>, consisting of shorts.
  409. Thus, <code>printf</code> may be called with the original values of
  410. <code>a[0]</code> and <code>a[1]</code>. What really happens is up to
  411. the compiler and may change with architecture and optimization level.</p>
  412. <p>Recent versions of GCC turn on the option <code>-fstrict-aliasing</code>
  413. (which allows alias-based optimizations) by default with <code>-O2</code>.
  414. And some architectures then really print "1111 1111" as result. Without
  415. optimization the executable will generate the "expected" output
  416. "2222 2222".</p>
  417. <p>To disable optimizations based on alias-analysis for faulty legacy code,
  418. the option <code>-fno-strict-aliasing</code> can be used as a work-around.</p>
  419. <p>The option <code>-Wstrict-aliasing</code> (which is included in
  420. <code>-Wall</code>) warns about some - but not all - cases of violation
  421. of aliasing rules when <code>-fstrict-aliasing</code> is active.</p>
  422. <p>To fix the code above, you can use a <code>union</code> instead of a
  423. cast (note that this is a GCC extension which might not work with other
  424. compilers):</p>
  425. <blockquote><pre>
  426. #include &lt;stdio.h&gt;
  427. int main()
  428. {
  429. union
  430. {
  431. short a[2];
  432. int i;
  433. } u;
  434. u.a[0]=0x1111;
  435. u.a[1]=0x1111;
  436. u.i = 0x22222222;
  437. printf("%x %x\n", u.a[0], u.a[1]);
  438. return 0;
  439. }
  440. </pre></blockquote>
  441. <p>Now the result will always be "2222 2222".</p>
  442. <p>For some more insight into the subject, please have a look at
  443. <a href="http://mail-index.NetBSD.org/tech-kern/2003/08/11/0001.html">this
  444. article</a>.</p></dd>
  445. <dt>Cannot use preprocessor directive in macro arguments.</dt>
  446. <dd><p>Let me guess... you used an older version of GCC to compile code
  447. that looks something like this:</p>
  448. <blockquote><pre>
  449. memcpy(dest, src,
  450. #ifdef PLATFORM1
  451. 12
  452. #else
  453. 24
  454. #endif
  455. );
  456. </pre></blockquote>
  457. <p>and you got a whole pile of error messages:</p>
  458. <blockquote><pre>
  459. test.c:11: warning: preprocessing directive not recognized within macro arg
  460. test.c:11: warning: preprocessing directive not recognized within macro arg
  461. test.c:11: warning: preprocessing directive not recognized within macro arg
  462. test.c: In function `foo':
  463. test.c:6: undefined or invalid # directive
  464. test.c:8: undefined or invalid # directive
  465. test.c:9: parse error before `24'
  466. test.c:10: undefined or invalid # directive
  467. </pre></blockquote>
  468. <p>This is because your C library's <code>&lt;string.h&gt;</code> happens
  469. to define <code>memcpy</code> as a macro - which is perfectly legitimate.
  470. In recent versions of glibc, for example, <code>printf</code> is among those
  471. functions which are implemented as macros.</p>
  472. <p>Versions of GCC prior to 3.3 did not allow you to put <code>#ifdef</code>
  473. (or any other preprocessor directive) inside the arguments of a macro. The
  474. code therefore would not compile.</p>
  475. <p>As of GCC 3.3 this kind of construct is always accepted and the
  476. preprocessor will probably do what you expect, but see the manual for
  477. detailed semantics.</p>
  478. <p>However, this kind of code is not portable. It is "undefined behavior"
  479. according to the C standard; that means different compilers may do
  480. different things with it. It is always possible to rewrite code which
  481. uses conditionals inside macros so that it doesn't. You could write
  482. the above example</p>
  483. <blockquote><pre>
  484. #ifdef PLATFORM1
  485. memcpy(dest, src, 12);
  486. #else
  487. memcpy(dest, src, 24);
  488. #endif
  489. </pre></blockquote>
  490. <p>This is a bit more typing, but I personally think it's better style
  491. in addition to being more portable.</p></dd>
  492. <dt>Cannot initialize a static variable with <code>stdin</code>.</dt>
  493. <dd><p>This has nothing to do with GCC, but people ask us about it a
  494. lot. Code like this:</p>
  495. <blockquote><pre>
  496. #include &lt;stdio.h&gt;
  497. FILE *yyin = stdin;
  498. </pre></blockquote>
  499. <p>will not compile with GNU libc, because <code>stdin</code> is not a
  500. constant. This was done deliberately, to make it easier to maintain
  501. binary compatibility when the type <code>FILE</code> needs to be changed.
  502. It is surprising for people used to traditional Unix C libraries, but it
  503. is permitted by the C standard.</p>
  504. <p>This construct commonly occurs in code generated by old versions of
  505. lex or yacc. We suggest you try regenerating the parser with a
  506. current version of flex or bison, respectively. In your own code, the
  507. appropriate fix is to move the initialization to the beginning of
  508. main.</p>
  509. <p>There is a common misconception that the GCC developers are
  510. responsible for GNU libc. These are in fact two entirely separate
  511. projects; please check the
  512. <a href="http://www.gnu.org/software/glibc/">GNU libc web pages</a>
  513. for details.
  514. </p></dd>
  515. </dl>
  516. <hr />
  517. <h2><a name="nonbugs_cxx">C++</a></h2>
  518. <dl>
  519. <dt>Nested classes can access private members and types of the containing
  520. class.</dt>
  521. <dd><p>Defect report 45 clarifies that nested classes are members of the
  522. class they are nested in, and so are granted access to private members of
  523. that class.</p></dd>
  524. <dt>G++ emits two copies of constructors and destructors.</dt>
  525. <dd><p>In general there are <em>three</em> types of constructors (and
  526. destructors).</p>
  527. <ol>
  528. <li>The complete object constructor/destructor.</li>
  529. <li>The base object constructor/destructor.</li>
  530. <li>The allocating constructor/deallocating destructor.</li>
  531. </ol>
  532. <p>The first two are different, when virtual base classes are involved.
  533. </p></dd>
  534. <dt>Global destructors are not run in the correct order.</dt>
  535. <dd><p>Global destructors should be run in the reverse order of their
  536. constructors <em>completing</em>. In most cases this is the same as
  537. the reverse order of constructors <em>starting</em>, but sometimes it
  538. is different, and that is important. You need to compile and link your
  539. programs with <code>--use-cxa-atexit</code>. We have not turned this
  540. switch on by default, as it requires a <code>cxa</code> aware runtime
  541. library (<code>libc</code>, <code>glibc</code>, or equivalent).</p></dd>
  542. <dt>Classes in exception specifiers must be complete types.</dt>
  543. <dd><p>[15.4]/1 tells you that you cannot have an incomplete type, or
  544. pointer to incomplete (other than <code><i>cv</i> void *</code>) in
  545. an exception specification.</p></dd>
  546. <dt>Exceptions don't work in multithreaded applications.</dt>
  547. <dd><p>You need to rebuild g++ and libstdc++ with
  548. <code>--enable-threads</code>. Remember, C++ exceptions are not like
  549. hardware interrupts. You cannot throw an exception in one thread and
  550. catch it in another. You cannot throw an exception from a signal
  551. handler and catch it in the main thread.</p></dd>
  552. <dt>Templates, scoping, and digraphs.</dt>
  553. <dd><p>If you have a class in the global namespace, say named <code>X</code>,
  554. and want to give it as a template argument to some other class, say
  555. <code>std::vector</code>, then <code>std::vector&lt;::X&gt;</code>
  556. fails with a parser error.</p>
  557. <p>The reason is that the standard mandates that the sequence
  558. <code>&lt;:</code> is treated as if it were the token <code>[</code>.
  559. (There are several such combinations of characters - they are called
  560. <em>digraphs</em>.) Depending on the version, the compiler then reports
  561. a parse error before the character <code>:</code> (the colon before
  562. <code>X</code>) or a missing closing bracket <code>]</code>.</p>
  563. <p>The simplest way to avoid this is to write <code>std::vector&lt;
  564. ::X&gt;</code>, i.e. place a space between the opening angle bracket
  565. and the scope operator.</p></dd>
  566. </dl>
  567. <h3><a name="updating">Common problems updating from G++ 2.95 to G++
  568. 3.0</a></h3>
  569. <p>G++ 3.0 conforms much closer to the ISO C++ standard (available at
  570. <a href="http://www.ncits.org/cplusplus.htm">http://www.ncits.org/cplusplus.htm</a>).</p>
  571. <p>We have also implemented some of the core and library defect reports
  572. (available at
  573. <a href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html">http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html</a>
  574. &amp;
  575. <a href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html">
  576. http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html</a>
  577. respectively).</p>
  578. <ul>
  579. <li>The ABI has changed. This means that both class layout and name
  580. mangling is different. You <em>must</em> recompile all c++ libraries (if
  581. you don't you will get link errors).</li>
  582. <li>The standard library is much more conformant, and uses the
  583. <code>std::</code> namespace.</li>
  584. <li><code>std::</code> is now a real namespace, not an alias for
  585. <code>::</code>.</li>
  586. <li>The standard header files for the c library don't end with
  587. <code>.h</code>, but begin with <code>c</code> (i.e.
  588. <code>&lt;cstdlib&gt;</code> rather than <code>&lt;stdlib.h&gt;</code>).
  589. The <code>.h</code> names are still available, but are deprecated.</li>
  590. <li><code>&lt;strstream&gt;</code> is deprecated, use
  591. <code>&lt;sstream&gt;</code> instead.</li>
  592. <li><code>streambuf::seekoff</code> &amp;
  593. <code>streambuf::seekpos</code> are private, instead use
  594. <code>streambuf::pubseekoff</code> &amp;
  595. <code>streambuf::pubseekpos</code> respectively.</li>
  596. <li>If <code>std::operator &lt;&lt; (std::ostream &amp;, long long)</code>
  597. doesn't exist, you need to recompile libstdc++ with
  598. <code>--enable-long-long</code>.</li>
  599. </ul>
  600. <p>This means you may get lots of errors about things like
  601. <code>strcmp</code> not being found. You've most likely forgotten to
  602. tell the compiler to look in the <code>std::</code> namespace. There are
  603. several ways to do this,</p>
  604. <ul>
  605. <li>Say, <code>std::strcmp</code> at the call. This is the most explicit
  606. way of saying what you mean.</li>
  607. <li>Say, <code>using std::strcmp;</code> somewhere before the call. You
  608. will need to do this for each function or type you wish to use from the
  609. standard library.</li>
  610. <li>Say, <code>using namespace std;</code> somewhere before the call.
  611. This is the quick-but-dirty fix. This brings the <em>whole</em> of the
  612. <code>std::</code> namespace into scope. <em>Never</em> do this in a
  613. header file, as you will be forcing users of your header file to do the
  614. same.</li>
  615. </ul>
  616. <p>In addition to the problems listed above, the manual contains a section on
  617. <a href="http://gcc.gnu.org/onlinedocs/gcc/C---Misunderstandings.html">Common
  618. Misunderstandings with GNU C++</a>.</p>
  619. </body>
  620. </html>