g++faq.texi
来自「GCC编译器源代码」· TEXI 代码 · 共 1,691 行 · 第 1/5 页
TEXI
1,691 行
in the official distribution, due to a disagreement over design philosophies.But you can get a patch from Cygnus to turn it on; retrieve the patchfrom @file{ftp://ftp.cygnus.com/pub/g++/gcc-2.7.2-repo.gz} to patchgcc-2.7.2 (there are also patches for earlier gcc versions).@item@cindex exception handling, 2.7.0@xref{exceptions}@item@cindex run-time type identificationSupport for Run-Time Type Identification has been added with @code{-frtti}.This support is still in alpha; one major restriction is that any filecompiled with @code{-frtti} must include @code{<typeinfo>} (@emph{not}@code{typeinfo.h} as the NEWS file says).Also, all C++ code you link with (including libg++) has to be built with@code{-frtti}, so it's still tricky to use.@item@cindex compiler-generated operatorsSynthesis of compiler-generated constructors, destructors andassignment operators is now deferred until the functions are used.@item@cindex assignment in conditional expressionsThe parsing of expressions such as @code{a ? b : c = 1}has changed from@code{(a ? b : c) = 1} to @code{a ? b : (c = 1)}. This is a new C/C++incompatibility brought to you by the ANSI/ISO standards committee.@item@cindex new operator keywordsThe operator keywords and, and_eq, bitand, bitor, compl, not, not_eq,or, or_eq, xor and xor_eq are now supported. Use @code{-ansi} or@code{-foperator-names} to enable them.@item@cindex explicit keywordThe @code{explicit} keyword is now supported. @code{explicit} is used to markconstructors and type conversion operators that should not be usedimplicitly.@item@cindex user-defined type conversionHandling of user-defined type conversion has been improved.@item@cindex explicit template instantiationExplicit instantiation of template methods is now supported. Also,@code{inline template class foo<int>;}can be used to emit only the vtablefor a template class.@item@cindex -fcheck-newWith -fcheck-new, g++ will check the return value of all calls tooperator new, and not attempt to modify a returned null pointer.@itemcollect2 now demangles linker output, and c++filt has become part ofthe gcc distribution.@itemImprovements to template instantiation: only members actually usedare instantiated. (Actually this is not quite true: some inlinetemplates that are not successfully inlined may be expanded eventhough they are not needed).@end itemize@node libstdc++, new work, version 2.7.x, evolution@section The GNU Standard C++ LibraryThe GNU Standard C++ Library (also called the ``GNU ANSI C++ Library''in places in the code) is not libg++, though it is included in thelibg++ distribution. Rather, it contains classes and functionsrequired by the ANSI/ISO standard. The copyright conditions are thesame as those for for the iostreams classes; the LGPL is not used(@pxref{legalities}).This library, libstdc++, is in the libg++ distribution in versions 2.6.2and later. It requires at least gcc 2.6.3 to build the libg++-2.6.2version; use at least gcc 2.7.0 to build the libg++ 2.7.0 version. Itcontains a hacked-up version of HP's implementation of the StandardTemplate Library (@pxref{Standard Template Library}). I'vesuccessfully used this Standard Template Library version to builda number of the demos you'll see on various web pages.As of version 2.7.0, the streams classes are now in libstdc++ instead oflibg++, and libiostream is being phased out (don't use it). The g++program searches this library.The maintainers of libg++ have de-emphasized work on the older libg++ classesin favor of enhancing libstdc++ to cover the full language, so while libg++will always be available, enhancements to it should not be expected.@node new work, egcs, libstdc++, evolution@section What can we expect in future gcc releases?A great deal of work has gone into enhancements to the C++ front end, as wellas to other aspects of the compiler.The next major release(s) of gcc/g++ can be expected to have the followingfeatures:@itemize @bullet@cindex new template implementation@itemA completely new template implementation, much closer to the draftstandard. Limitations in 2.7.2.x concerning inlining template functionswill be eliminated. Static template data members, template class memberfunctions, partial specification, and default template arguments will besupported. An instantiation method resembling that used in Borland C++(instantiating functions possibly in multiple .o files and using weaksymbols to link correctly) will be provided, in addition to otheroptions. The SGI version of STL will be shipped with libstdc++ and willcompile unchanged.@item@cindex new exception implementationException handling has been re-worked; exceptions will work togetherwith optimization.Actually, there are two separate implementations: one based on setjmp/longjmpand designed to be highly portable, and one designed to be more efficient butrequiring more processor-specific support (getting exceptions right has provento be extremely difficult and has been the chief obstacle to getting a newrelease out).@item@cindex RTTIRTTI has been re-done to work correctly and is on by default.@item@cindex overloadingOverloading has been re-worked to conform to the latest draft of thestandard.@end itemizeFeatures that are still missing include namespaces and templates astemplate arguments.@node egcs, When?, new work, evolution@section What's this I hear about egcs?The egcs effort is a new effort to merge several threads of gccdevelopment and to provide a faster development process.For more information see @file{http://www.cygnus.com/egcs/}.@node When?, , egcs, evolution@section OK, when can I get this stuff?The FSF has a policy of never announcing release dates in advance.I'm sure this is frustrating to a lot of people, since it's takenso long, and this frustration was one of the reasons the egcs effortwas created. An egcs release should be expected to occur in thevery near future. [ More on this next time ].@node User Problems, legalities, evolution, Top@chapter User Problems@menu* missing virtual table:: * for scope:: * const constructor:: * unused parameter warnings:: * jump crosses initialization:: * Demangler:: * static data members:: * internal compiler error:: * bug reports:: * porting to g++:: * name mangling:: * problems linking with other libraries:: * documentation:: * templates:: * undefined templates:: * redundant templates:: * Standard Template Library:: * STL and string:: * exceptions:: * namespaces:: * agreement with standards:: * compiling standard libraries:: * debugging on SVR4 systems:: * debugging problems on Solaris:: * X11 conflicts with libg++:: * assignment to streams:: @end menu@node missing virtual table, for scope, User Problems, User Problems@section Linker complains about missing virtual table``I'm getting a message complaining about an undefined virtual table. Isthis a compiler bug?''(On platforms that run neither collect nor the GNU linker, like Solaris,you may see an odd undefined symbol like "_vt.3foo", where foo is aclass name).This is probably because you are missing a definition for the first(non-inline) virtual function of the class. Since gcc-2.7.0, g++ usesa trick borrowed from cfront: the .o file containing the definition forthe first non-inline virtual function for the class will also containthe virtual function table.@node for scope, const constructor, missing virtual table, User Problems@section gcc-2.7.0 breaks declarations in "for" statements!@cindex declarations in for statements@cindex for statements: declarationsgcc-2.7.0 implements the new ANSI/ISO rule on the scope of variablesdeclared in for loops.@examplefor (int i = 1; i <= 10; i++) @{ // do something here@}foo(i);@end exampleIn the above example, most existing C++ compilers would pass thevalue 11 to the function @code{foo}. In gcc 2.7 and in the ANSI/ISOworking paper, the scope of @code{i} is only the for loop body, sothis is an error. So that old code can be compiled, the new gcc hasa flag @code{-fno-for-scope} that causes the old rule to be used.@cindex -fno-for-scopeAs of 2.7.1, the compiler attempts to issue warnings about code thathas different meanings under the two sets of rules, but the code isnot perfect: the intent was that code that has valid, but different,meanings under the ARM rules and the working paper rules would givewarnings but have the new behavior, and this doesn't seem to happen.The @code{-ffor-scope} flag under 2.7.1 and 2.7.2 gives the 2.7.0 behavior.@node const constructor, unused parameter warnings, for scope, User Problems@section g++ seems to want a const constructor. What's that?gcc-2.7.1 introduced a bug that causes the compiler to ask for aconst constructor (there's no such thing in C++) in certain situationswhere a const object appears in a template class. Most cases have beenfixed in gcc-2.7.2, but unfortunately not all. Still, if you're runninggcc-2.7.1 and have this problem, upgrade to 2.7.2; it is a vast improvement.@cindex ObjectSpace<STL>The default constructor for the template @code{pair} in ObjectSpace'simplementation of STL triggers the bug in one place, for gcc 2.7.2. Ifyou're using ObjectSpace<STL> and having this problem, simplychange the default constructor from@exampleos_pair () : first (T1 ()), second (T2 ()) @{@}@end exampleto just@exampleos_pair () @{@}@end exampleOnce this is done, ObjectSpace<STL> works fairly well.@node unused parameter warnings, jump crosses initialization, const constructor, User Problems@section How to silence ``unused parameter'' warnings@cindex -Wall@cindex -Wunused``When I use @code{-Wall} (or @code{-Wunused}), g++ warns aboutunused parameters. But the parameters have to be there, for usein derived class functions. How do I get g++ to stop complaining?''The answer is to simply omit the names of the unused parameters whendefining the function. This makes clear, both to g++ and to readersof your code, that the parameter is unused. For example:@exampleint Foo::bar(int arg) @{ return 0; @}@end examplewill give a warning for the unused parameter @code{arg}. To suppressthe warning write@exampleint Foo::bar(int) @{ return 0; @}@end example@node jump crosses initialization, Demangler, unused parameter warnings, User Problems@section g++ objects to a declaration in a case statement``The compiler objects to my declaring a variable in one of the branchesof a case statement. Earlier versions used to accept this code. Why?''The draft standard does not allow a goto or a jump to a case label toskip over an initialization of a variable or a class object. Forexample:@exampleswitch ( i ) @{ case 1: Object obj(0); ... break; case 2: ... break;@} @end exampleThe reason is that @code{obj} is also in scope in the rest of the switchstatement.As of version 2.7.0, the compiler will object that the jump to thesecond case level crosses the initialization of @code{obj}. Oldercompiler versions would object only if class Object has a destructor.In either case, the solution is to add a set of curly braces aroundthe case branch:@example case 1: @{ Object obj(0); ... break; @}@end example@node Demangler, static data members, jump crosses initialization, User Problems@section Where can I find a demangler?@cindex demangler programA g++-compatible demangler named @code{c++filt} can be found in the@file{binutils} distribution. This distribution (which also containsthe GNU linker) can be found at any GNU archive site.As of version 2.7.0, @code{c++filt} is included with gcc and is
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?