rhl28.htm

来自「linux的初学电子书」· HTM 代码 · 共 2,643 行 · 第 1/4 页

HTM
2,643
字号

-fnonnull-objects

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Assumes that objects reached through references are not null.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fsave-memoized

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Same as -fmemorize-lookups.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fthis-is-variable

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Permits assignment to &quot;this.&quot;</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-nostdinc++

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Does not search for header files in the standard directories specific to C++.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-traditional

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

This option has the same effect as -fthis-is-variable, but also attempts to support some aspects of traditional C compilers.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fno-default-inline

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Does not assume that functions defined within a class scope are inline functions.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-Wenum-clash

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Warns about conversion between different enumeration types.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-Woverloaded-virtual

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

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.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-Wtemplate-debugging

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

If you are using templates, this option warns you if debugging is not yet available.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-gstabs+

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

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 gdb handles C++ programs properly.</FONT>

</TABLE><BR>

<A NAME="E68E227"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Debugging C++ Applications</B></FONT></CENTER></H3>

<BR>

<P>A very important part of developing C++ programs is being able to debug them efficiently. The GNU debug application that was introduced in <A HREF="rhl27.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl27.htm">Chapter 27</A> can also be used to debug C++ applications. This section describes some of the 
differences between debugging C applications and debugging C++ applications.

<BR>

<P>The basic gdb commands that were introduced in <A HREF="rhl27.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl27.htm">Chapter 27</A> are listed again for your convenience in Table 28.2.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 28.2. Basic </B><B>gdb</B><B> commands.</B></FONT></CENTER>

<BR>



<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Command</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Description</I></FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

file

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Loads the executable file that is to be debugged.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

kill

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Terminates the program you are currently debugging.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

list

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Lists sections of the source code used to generate the executable file.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

next

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Advances one line of source code in the current function, without stepping into other functions.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

step

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Advances one line of source code in the current function, and does step into other functions.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

run

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Executes the program that is currently being debugged.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

quit

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Terminates gdb.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

watch

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Enables you to examine the value of a program variable whenever the value changes.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

break

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Sets a breakpoint in the code; this causes the execution of the program to be suspended whenever this point is reached.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

make

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

This command enables you to remake the executable program without quitting gdb or using another window.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

shell

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Enables you to execute UNIX shell commands without leaving gdb.</FONT>

</TABLE><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. gdb has added features to 
support debugging both of these C++ specific features.

<BR>

<BR>

<A NAME="E69E350"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Debugging Virtual Functions</B></FONT></CENTER></H4>

<BR>

<P>As described in the &quot;Polymorphism&quot; section of 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

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">void func(int, real);</FONT></PRE>

<P>has a signature of int,real.

<BR>

<P>You can see how this could cause gdb a few problems. For example, if you had defined a class that had a virtual function called calculate, 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 gdb break command, as follows:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080"> (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 calculate you wanted the breakpoint to be set on. gdb 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:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">break 'calculate (float)'</FONT></PRE>

<P>This would give gdb enough information to determine which function the breakpoint was meant for. A second solution that gdb 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 break command. The second choice is to set a breakpoint on all the functions that the break command matches. The remaining choices 
correspond to each function that matches the break command. The following code shows an example of a breakpoint menu:

<BR>

<PRE>

<FONT COLOR="#000080">(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

&gt; 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 &quot;delete&quot; command to delete unwanted breakpoints

 (gdb)</FONT></PRE>

<BR>

<A NAME="E69E351"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Debugging Exception Handlers</B></FONT></CENTER></H4>

<BR>

<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 malloc function to get a block of memory, 
you would typically check malloc'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 malloc function would send or throw an 
exception to your function if one occurred.

<BR>

<P>The gdb added two new commands to support C++ exception handling: the catch command and the catch info command. The catch command is used to set a breakpoint in active exception handlers. The syntax of this command is as follows:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">catch exceptions</FONT></PRE>

<P>exceptions is a list of the exceptions to catch.

<BR>

<P>The catch info command is used to display all the active exception handlers.

<BR>

<BR>

<A NAME="E69E352"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Summary of </B><B>gdb</B><B> C++ Specific Commands</B></FONT></CENTER></H4>

<BR>

<P>In addition to the gdb commands that have been added to support some of the new language features contained in C++, there are also some new set and show options. These options are listed in Table 28.3.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 28.3. </B><B>gdb's</B><B> C++ </B><B>set</B><B> and </B><B>show</B><B> options.</B></FONT></CENTER>

<BR>



<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Command</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Description</I></FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

set print demangle

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Prints C++ names in their source form rather than in the encoded or mangled form that is passed to the assembler.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

show print demangle

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Shows whether print demangle is on or off.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

set demangle-style

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Sets the style of demangled output. The options are auto, gnu, lucid, and arm.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

show demangle-style

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Shows which demangle style is being used.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

set print object

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When displaying a pointer to an object, identifies the actual type of the object.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

show print object

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Shows whether print object is turned on or off.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

set print vtbl

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Pretty prints C++ virtual function tables.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

show print vtbl

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Shows whether print vtbl is turned on or off.</FONT>

</TABLE><BR>

<A NAME="E68E228"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>GNU C++ Class Libraries</B></FONT></CENTER></H3>

<BR>

<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 examples of class libraries are class libraries that handle database access, 
class libraries that handle graphical user interface programming, and class libraries that implement data structures.

<BR>

<P>Examples of graphical user interface class libraries include the Microsoft Foundation Classes and Borland's Object Windows Library, both of which are class libraries that are used for developing Windows applications.

<BR>

<P>This section introduces several of the features that are offered by the GNU C++ class library.

<BR>

<BR>

<A NAME="E69E353"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Streams</B></FONT></CENTER></H4>

<BR>

<P>The GNU iostream library, called libio, implements GNU C++'s standard input and output facilities. This library is similar to the I/O libraries that are supplied by other C++ compilers. The main parts of the iostream library are the input, output, and 
error streams. These correspond to the standard input, output, and error streams that are found in C and are called cin, cout, and cerr respectively. The streams can be written to and read from using the &lt;&lt; operator for output and the &gt;&gt; 
operator for input.

<BR>

<P>The following program uses the iostream library to perform its input and output:

<BR>

<PRE>

<FONT COLOR="#000080">#include &lt;iostream.h&gt;

int main ()

{

char name[10];

cout &lt;&lt; &quot;Please enter your name.\n&quot;;

cin &gt;&gt; name;

cout &lt;&lt; &quot;Hello, &quot; &lt;&lt; name &lt;&lt; &quot;, how is it going?\n&quot;;

}</FONT></PRE>

<BR>

<A NAME="E69E354"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Strings</B></FONT></CENTER></H4>

<BR>

<P>The GNU string class extends GNU C++'s string manipulation capabilities. The stringclass essentially replaces the character array definitions that existed in C and all the stringfunctions that go along with the character arrays.

<BR>

<P>The string class adds UNIX shell type string operators to the C++ language, as well as a large number of additional operators. Table 28.4 lists many of the operators that are available with the string class.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 28.4. String class operators.</B></FONT></CENTER>

<BR>



<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Operator</I>

⌨️ 快捷键说明

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