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