gcc.texi

来自「GCC编译器源代码」· TEXI 代码 · 共 1,739 行 · 第 1/5 页

TEXI
1,739
字号
Many user programs contain the declaration @samp{long time ();}.  In thepast, the system header files on many systems did not actually declare@code{time}, so it did not matter what type your program declared it toreturn.  But in systems with ANSI C headers, @code{time} is declared toreturn @code{time_t}, and if that is not the same as @code{long}, then@samp{long time ();} is erroneous.The solution is to change your program to use @code{time_t} as the returntype of @code{time}.@cindex @code{float} as function value type@itemWhen compiling functions that return @code{float}, PCC converts it toa double.  GNU CC actually returns a @code{float}.  If you are concernedwith PCC compatibility, you should declare your functions to return@code{double}; you might as well say what you mean.@cindex structures@cindex unions@itemWhen compiling functions that return structures or unions, GNU CCoutput code normally uses a method different from that used on mostversions of Unix.  As a result, code compiled with GNU CC cannot calla structure-returning function compiled with PCC, and vice versa.The method used by GNU CC is as follows: a structure or union which is1, 2, 4 or 8 bytes long is returned like a scalar.  A structure or unionwith any other size is stored into an address supplied by the caller(usually in a special, fixed register, but on some machines it is passedon the stack).  The machine-description macros @code{STRUCT_VALUE} and@code{STRUCT_INCOMING_VALUE} tell GNU CC where to pass this address.By contrast, PCC on most target machines returns structures and unionsof any size by copying the data into an area of static storage, and thenreturning the address of that storage as if it were a pointer value.The caller must copy the data from that memory area to the place wherethe value is wanted.  GNU CC does not use this method because it isslower and nonreentrant.On some newer machines, PCC uses a reentrant convention for allstructure and union returning.  GNU CC on most of these machines uses acompatible convention when returning structures and unions in memory,but still returns small structures and unions in registers.You can tell GNU CC to use a compatible convention for all structure andunion returning with the option @samp{-fpcc-struct-return}.@cindex preprocessing tokens@cindex preprocessing numbers@itemGNU C complains about program fragments such as @samp{0x74ae-0x4000}which appear to be two hexadecimal constants separated by the minusoperator.  Actually, this string is a single @dfn{preprocessing token}.Each such token must correspond to one token in C.  Since this does not,GNU C prints an error message.  Although it may appear obvious that whatis meant is an operator and two values, the ANSI C standard specificallyrequires that this be treated as erroneous.A @dfn{preprocessing token} is a @dfn{preprocessing number} if itbegins with a digit and is followed by letters, underscores, digits,periods and @samp{e+}, @samp{e-}, @samp{E+}, or @samp{E-} charactersequences.To make the above program fragment valid, place whitespace in front ofthe minus sign.  This whitespace will end the preprocessing number.@end itemize@node Fixed Headers@section Fixed Header FilesGNU CC needs to install corrected versions of some system header files.This is because most target systems have some header files that won'twork with GNU CC unless they are changed.  Some have bugs, some areincompatible with ANSI C, and some depend on special features of othercompilers.Installing GNU CC automatically creates and installs the fixed headerfiles, by running a program called @code{fixincludes} (or for certaintargets an alternative such as @code{fixinc.svr4}).  Normally, youdon't need to pay attention to this.  But there are cases where itdoesn't do the right thing automatically.@itemize @bullet@itemIf you update the system's header files, such as by installing a newsystem version, the fixed header files of GNU CC are not automaticallyupdated.  The easiest way to update them is to reinstall GNU CC.  (Ifyou want to be clever, look in the makefile and you can find ashortcut.)@itemOn some systems, in particular SunOS 4, header file directories containmachine-specific symbolic links in certain places.  This makes itpossible to share most of the header files among hosts running thesame version of SunOS 4 on different machine models.The programs that fix the header files do not understand this specialway of using symbolic links; therefore, the directory of fixed headerfiles is good only for the machine model used to build it.In SunOS 4, only programs that look inside the kernel will notice thedifference between machine models.  Therefore, for most purposes, youneed not be concerned about this.It is possible to make separate sets of fixed header files for thedifferent machine models, and arrange a structure of symbolic links soas to use the proper set, but you'll have to do this by hand.@itemOn Lynxos, GNU CC by default does not fix the header files.  This isbecause bugs in the shell cause the @code{fixincludes} script to fail.This means you will encounter problems due to bugs in the system headerfiles.  It may be no comfort that they aren't GNU CC's fault, but itdoes mean that there's nothing for us to do about them.@end itemize@node Standard Libraries@section Standard LibrariesGNU CC by itself attempts to be what the ISO/ANSI C standard calls a@dfn{conforming freestanding implementation}.  This means all ANSIC language features are available, as well as the contents of@file{float.h}, @file{limits.h}, @file{stdarg.h}, and@file{stddef.h}.  The rest of the C library is supplied by thevendor of the operating system.  If that C library doesn't conform tothe C standards, then your programs might get warnings (especially whenusing @samp{-Wall}) that you don't expect.For example, the @code{sprintf} function on SunOS 4.1.3 returns@code{char *} while the C standard says that @code{sprintf} returns an@code{int}.  The @code{fixincludes} program could make the prototype forthis function match the Standard, but that would be wrong, since thefunction will still return @code{char *}.If you need a Standard compliant library, then you need to find one, asGNU CC does not provide one.  The GNU C library (called @code{glibc})has been ported to a number of operating systems, and provides ANSI/ISO,POSIX, BSD and SystemV compatibility.  You could also ask your operatingsystem vendor if newer libraries are available.@node Disappointments@section Disappointments and MisunderstandingsThese problems are perhaps regrettable, but we don't know any practicalway around them.@itemize @bullet@itemCertain local variables aren't recognized by debuggers when you compilewith optimization.This occurs because sometimes GNU CC optimizes the variable out ofexistence.  There is no way to tell the debugger how to compute thevalue such a variable ``would have had'', and it is not clear that wouldbe desirable anyway.  So GNU CC simply does not mention the eliminatedvariable when it writes debugging information.You have to expect a certain amount of disagreement between theexecutable and your source code, when you use optimization.@cindex conflicting types@cindex scope of declaration@itemUsers often think it is a bug when GNU CC reports an error for codelike this:@exampleint foo (struct mumble *);struct mumble @{ @dots{} @};int foo (struct mumble *x)@{ @dots{} @}@end exampleThis code really is erroneous, because the scope of @code{structmumble} in the prototype is limited to the argument list containing it.It does not refer to the @code{struct mumble} defined with file scopeimmediately below---they are two unrelated types with similar names indifferent scopes.But in the definition of @code{foo}, the file-scope type is usedbecause that is available to be inherited.  Thus, the definition andthe prototype do not match, and you get an error.This behavior may seem silly, but it's what the ANSI standard specifies.It is easy enough for you to make your code work by moving thedefinition of @code{struct mumble} above the prototype.  It's not worthbeing incompatible with ANSI C just to avoid an error for the exampleshown above.@itemAccesses to bitfields even in volatile objects works by accessing largerobjects, such as a byte or a word.  You cannot rely on what size ofobject is accessed in order to read or write the bitfield; it may evenvary for a given bitfield according to the precise usage.If you care about controlling the amount of memory that is accessed, usevolatile but do not use bitfields.@itemGNU CC comes with shell scripts to fix certain known problems in systemheader files.  They install corrected copies of various header files ina special directory where only GNU CC will normally look for them.  Thescripts adapt to various systems by searching all the system headerfiles for the problem cases that we know about.If new system header files are installed, nothing automatically arrangesto update the corrected header files.  You will have to reinstall GNU CCto fix the new header files.  More specifically, go to the builddirectory and delete the files @file{stmp-fixinc} and@file{stmp-headers}, and the subdirectory @code{include}; then do@samp{make install} again.@item@cindex floating point precisionOn 68000 and x86 systems, for instance, you can get paradoxical resultsif you test the precise values of floating point numbers.  For example,you can find that a floating point value which is not a NaN is not equalto itself.  This results from the fact that the floating point registershold a few more bits of precision than fit in a @code{double} in memory.Compiled code moves values between memory and floating point registersat its convenience, and moving them into memory truncates them.You can partially avoid this problem by using the @samp{-ffloat-store}option (@pxref{Optimize Options}).@itemOn the MIPS, variable argument functions using @file{varargs.h}cannot have a floating point value for the first argument.  Thereason for this is that in the absence of a prototype in scope,if the first argument is a floating point, it is passed in afloating point register, rather than an integer register.If the code is rewritten to use the ANSI standard @file{stdarg.h}method of variable arguments, and the prototype is in scope atthe time of the call, everything will work fine.@itemOn the H8/300 and H8/300H, variable argument functions must beimplemented using the ANSI standard @file{stdarg.h} method ofvariable arguments.  Furthermore, calls to functions using @file{stdarg.h}variable arguments must have a prototype for the called functionin scope at the time of the call.@end itemize@node C++ Misunderstandings@section Common Misunderstandings with GNU C++@cindex misunderstandings in C++@cindex surprises in C++@cindex C++ misunderstandingsC++ is a complex language and an evolving one, and its standard definition(the ANSI C++ draft standard) is also evolving.  As a result,your C++ compiler may occasionally surprise you, even when its behavior iscorrect.  This section discusses some areas that frequently give rise toquestions of this sort.@menu* Static Definitions::  Static member declarations are not definitions* Temporaries::         Temporaries may vanish before you expect@end menu@node Static Definitions@subsection Declare @emph{and} Define Static Members@cindex C++ static data, declaring and defining@cindex static data in C++, declaring and defining@cindex declaring static data in C++@cindex defining static data in C++When a class has static data members, it is not enough to @emph{declare}the static member; you must also @emph{define} it.  For example:@exampleclass Foo@{  @dots{}  void method();  static int bar;@};@end exampleThis declaration only establishes that the class @code{Foo} has an@code{int} named @code{Foo::bar}, and a member function named@code{Foo::method}.  But you still need to define @emph{both}@code{method} and @code{bar} elsewhere.  According to the draft ANSIstandard, you must supply an initializer in one (and only one) sourcefile, such as:@exampleint Foo::bar = 0;@end exampleOther C++ compilers may not correctly implement the standard behavior.As a result, when you switch to @code{g++} from one of these compilers,you may discover that a program that appeared to work correctly in factdoes not conform to the standard: @code{g++} reports as undefinedsymbols any static data members that lack definitions.@node Temporaries@subsection Temporaries May Vanish Before You Expect@cindex temporaries, lifetime of@cindex portions of temporary objects, pointers toIt is dangerous to use pointers or references to @emph{portions} of atemporary object.  The compiler may very well delete the object beforeyou expect it to, leaving a pointer to garbage.  The most common placewhere this problem crops up is in classes like the libg++@code{String} class, that define a conversion function to type@code{char *} or @code{const char *}.  However, any class that returnsa pointer to some internal structure is potentially subject to thisproblem.For example, a program may use a function @code{strfunc} that returns@code{String} objects, and another function @code{charfunc} thatoperates on pointers to @code{char}:@exampleString strfunc ();void charfunc (const char *);@end example@noindentIn this situation, it may seem natural to write @w{@samp{charfunc(strfunc ());}} based on the knowledge that class @code{String} has anexplicit conversion to @code{char} pointers.  However, what reallyhappens is akin to @samp{charfunc (@w{strfunc ()}.@w{convert ()});},where the @code{convert} method is a function to do the same dataconversion normally performed by a cast.  Since the last use of thetemporary @code{String} object is the call to the conversion function,the compiler may delete that object before actually calling@code{charfunc}.  The compiler has no way of knowing that deleting the@code{String} object will invalidate the pointer.  The pointer thenpoints to garbage, so that by the time @code{charfunc} is called, itgets an invalid argument.Code like this may run successfully under some other compilers,especially those that delete temporaries relatively late.  However, theGNU C++ behavior is also standard-conforming, so if your program dependson late destruction of temporaries it is not portable.If you think this is surprising, you should be aware that the ANSI C++committee continues to debate the lifetime-of-temporaries problem.For now, at least, the safe way to write such code is to give thetemporary a name, which forces it to remain until the end of the s

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?