📄 ch28.htm
字号:
<TD ALIGN="LEFT" VALIGN="TOP">Treats all possible member functions as virtual. This applies to all functions except
for constructor functions and new or deleted member functions.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fdollars-in-identifiers</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Accepts $ in identifiers. You can also prohibit the use of $ in identifiers by using
the <TT>-fno-dollars-in-identifiers</TT> option.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-felide-constructors</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Tells the compiler to leave out constructors whenever possible.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fenum-int-equiv</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Permits implicit conversion of int to enumeration types.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fexternal-templates</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Produces smaller code for template declarations. This is done by having the compiler
generate only a single copy of each template function where it is defined.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fmemorize-lookups</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Uses heuristics to compile faster. These heuristics are not enabled by default because
they are effective only for certain input files.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fno-strict-prototype</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Treats a function declaration with no arguments the same way that C would treat it.
This means that the compiler treats a function prototype that has no arguments as
a function that will accept an unknown number of arguments.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fno-null-objects</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Assumes that objects reached through references are not null.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fsave-memorized</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Same as <TT>-fmemorize-lookups</TT>.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fthis-is-variable</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Permits assignment to "this."</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-nostdinc++</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Does not search for header files in the standard directories specific to C++.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-traditional</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">This option has the same effect as <TT>-fthis-is-variable</TT>.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fno-default-inline</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Does not assume that functions defined within a class scope are inline functions.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-wenum-clash</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Warns about conversion between different enumeration types.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-woverloaded-virtual</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Warns when derived class function declaration may be an error in defining a virtual
function. When you define a virtual function in a derived class, it must have the
same signature as the function in the base class. This option tells the compiler
to warn you if you have defined a function that has the same name and a different
signature as a function that is defined in one of the base classes.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-wtemplate-debugging</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">If you are using templates, this option warns you if debugging is not yet available.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>+eN</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Controls how virtual function definitions are used.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-gstabs+</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Tells the compiler to generate debugging information in stabs format, using GNU extensions
understood only by the GNU debugger. The extra information produced by this option
is necessary to ensure that <TT>gdb</TT> handles C++ programs properly.</TD>
</TR>
</TABLE>
</CENTER>
<CENTER>
<H3><A NAME="Heading11<FONT COLOR="#000077">Debugging C++ Applications</FONT></H3>
</CENTER>
<P>A very important part of developing C++ programs is being able to debug them efficiently.
The GNU debug application that was introduced in Chapter 27 can also be used to debug
C++ applications. This section describes some of the differences between debugging
C applications and debugging C++ applications.</P>
<P>The basic <TT>gdb</TT> commands that were introduced in Chapter 27 are listed
again for your convenience in Table 28.2. </P>
<CENTER>
<P><FONT SIZE="4"><B>Table 28.2.</B> Basic gdb commands. </FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><I>Command</I></TD>
<TD ALIGN="LEFT"><I>Description</I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>file</TT></TD>
<TD ALIGN="LEFT">Loads the executable file that is to be debugged.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>kill</TT></TD>
<TD ALIGN="LEFT">Terminates the program you are currently debugging.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>list</TT></TD>
<TD ALIGN="LEFT">Lists sections of the source code used to generate the executable file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>next</TT></TD>
<TD ALIGN="LEFT">Advances one line of source code in the current function, without stepping into other
functions.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>step</TT></TD>
<TD ALIGN="LEFT">Advances one line of source code in the current function, and does step into other
functions.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>run</TT></TD>
<TD ALIGN="LEFT">Executes the program that is currently being debugged.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>quit</TT></TD>
<TD ALIGN="LEFT">Terminates <TT>gdb</TT>.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>watch</TT></TD>
<TD ALIGN="LEFT">Enables you to examine the value of a program variable whenever the value changes.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>break</TT></TD>
<TD ALIGN="LEFT">Sets a breakpoint in the code; this causes the execution of the program to be suspended
whenever this point is reached.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>make</TT></TD>
<TD ALIGN="LEFT">Enables you to remake the executable program without quitting <TT>gdb</TT> or using
another window.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD WIDTH="86" ALIGN="LEFT"><TT>shell</TT></TD>
<TD ALIGN="LEFT">Enables you to execute UNIX shell commands without leaving <TT>gdb</TT>.</TD>
</TR>
</TABLE>
</P>
</CENTER>
<P>From the programmer's perspective, you have more details to be aware of when debugging
C++ code than when you are debugging C code. This is because of the C++ features
such as virtual functions and exception handling. <TT>gdb</TT> has added features
to support debugging both of these C++ specific features.
<CENTER>
<H4><A NAME="Heading12<FONT COLOR="#000077">Debugging Virtual Functions</FONT></H4>
</CENTER>
<P>As described in the "Polymorphism" section, earlier in this chapter,
virtual functions are C++'s way of implementing polymorphism. This means that there
may be more than one function in a program with the same name. The only way to tell
these functions apart is by their signatures. The signature of a function is composed
of the types of all the arguments to the function. For example, a function with the
prototype</P>
<PRE><FONT COLOR="#0066FF">void func(int, real);
</FONT></PRE>
<P>has a signature of <TT>int,real</TT>.</P>
<P>You can see how this could cause the <TT>gdb</TT> a few problems. For example,
if you had defined a class that had a virtual function called <TT>calculate</TT>,
and two objects with different definitions for this function were created, how would
you set a breakpoint to trigger on this function? You set breakpoints in C by specifying
the function name as an argument to the <TT>gdb</TT> <TT>break</TT> command, as follows:</P>
<PRE><FONT COLOR="#0066FF">(gdb) break calculate
</FONT></PRE>
<P>This does not work in the case of a virtual function because the debugger would
not be able to tell which <TT>calculate</TT> you wanted the breakpoint to be set
on. <TT>gdb</TT> was extended in a few ways so that it could handle virtual functions.
The first way to solve the problem is to enter the function name by specifying its
prototype as well. This would be done in the following way:</P>
<PRE><FONT COLOR="#0066FF">break `calculate (float)'
</FONT></PRE>
<P>This would give <TT>gdb</TT> enough information to determine which function the
breakpoint was meant for. A second solution that <TT>gdb</TT> supports is using a
breakpoint menu. Breakpoint menus allow you to specify the function name of a function.
If there is more than one function definition for that function, it gives you a menu
of choices. The first choice in the menu is to abort the <TT>break</TT> command.
The second choice is to set a breakpoint on all the functions that the <TT>break</TT>
command matches. The remaining choices correspond to each function that matches the
<TT>break</TT> command. The following code shows an example of a breakpoint menu:</P>
<PRE><FONT COLOR="#0066FF"> (gdb) break shape::calculate
[0] cancel
[1] all
[2] file: shapes.C: line number: 153
[3] file: shapes.C: line number: 207
[4] file: shapes.C: line number: 247
> 2 3
Breakpoint 1 at 0xb234: file shapes.C, line 153
Breakpoint 2 at 0xa435: file shapes.C, line 207
Multiple breakpoints were set
Use the "delete" command to delete unwanted breakpoints
(gdb)
</FONT></PRE>
<CENTER>
<H4><A NAME="Heading13<FONT COLOR="#000077">Debugging Exception Handlers</FONT></H4>
</CENTER>
<P>Exceptions are errors that occur within your program. Exception handlers are pieces
of code that are written to handle errors and potential errors. For example, if you
were writing a C program and calling the <TT>malloc</TT> function to get a block
of memory, you would typically check <TT>malloc</TT>'s return code to make sure the
memory allocation was successful. If C supported exception handling, you could specify
a function that would receive or catch exceptions, and the <TT>malloc</TT> function
would send or throw an exception to your function if one occurred.</P>
<P>The <TT>gdb</TT> added two new commands to support C++ exception handling: the
<TT>catch</TT> command and the <TT>catch info</TT> command. The <TT>catch</TT> command
is used to set a breakpoint in active exception handlers. The syntax of this command
is as follows:</P>
<PRE><FONT COLOR="#0066FF">catch exceptions
</FONT></PRE>
<P>exceptions is a list of the exceptions to catch.</P>
<P>The <TT>catch info</TT> command is used to display all the active exception handlers.
<CENTER>
<H4><A NAME="Heading14<FONT COLOR="#000077">Summary of gdb C++ Specific Commands</FONT></H4>
</CENTER>
<P>In addition to the <TT>gdb</TT> commands that have been added to support some
of the new language features contained in C++, there are also some new <TT>set</TT>
and <TT>show</TT> options. These options are listed in Table 28.3. </P>
<CENTER>
<P><FONT SIZE="4"><B>Table 28.3.</B> gdb's C++ set and show options. </FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><I>Command</I></TD>
<TD ALIGN="LEFT"><I>Description</I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>set print demangle</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Prints C++ names in their source form rather than in the encoded or mangled form
that is passed to the assembler.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>show print demangle</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Shows whether <TT>print demangle</TT> is on or off.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>set demangle-style</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Sets the style of demangled output. The options are <TT>auto</TT>, <TT>gnu</TT>,
<TT>lucid</TT>, and <TT>arm</TT>.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>show demangle-style</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Shows which demangle style is being used.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>set print object</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">When displaying a pointer to an object, identifies the actual type of the object.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>show print object</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Shows whether <TT>print object</TT> is turned on or off.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>set print vtbl</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Pretty prints C++ virtual function tables.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>show print vtbl</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Shows whether <TT>print vtbl</TT> is turned on or off.</TD>
</TR>
</TABLE>
</CENTER>
<CENTER>
<H3><A NAME="Heading15<FONT COLOR="#000077">GNU C++ Class Libraries</FONT></H3>
</CENTER>
<P>GNU C++ comes packaged with an extensive class library. A class library is a reusable
set of classes that can be used to perform a specified set of functions. Some typical
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -