rhl27.htm
来自「linux的初学电子书」· HTM 代码 · 共 2,014 行 · 第 1/4 页
HTM
2,014 行
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">main [program.c]</FONT></PRE>
<P>If the function was not in one of the files that was passed to calls, it does not know where that function lives and prints only the function name:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">printf</FONT></PRE>
<P>calls also makes note of recursive and static functions in its output. Recursive functions are represented in the following way:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">fact <<< recursive in factorial.c >>></FONT></PRE>
<P>Static functions are represented as follows:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">total [static in calculate.c]</FONT></PRE>
<P>As an example, assume that you executed calls with the following program as input:
<BR>
<PRE>
<FONT COLOR="#000080">##include <stdio.h>
void main ()
{
void my_print(char *);
void my_print2(char *);
char my_string[] = "hello there";
my_print (my_string);
my_print2(my_string);
}
void my_print (char *string)
{
printf ("The string is %s\n", string);
}
void my_print2 (char *string)
{
char *string2;
int size, size2, i;
size = strlen (string);
size2 = size -1;
string2 = (char *) malloc (size + 1);
for (i = 0; i < size; i++)
string2[size2 - i] = string[i];
string2[size] = '\0';
printf ("The string printed backward is %s\n", string2);
}</FONT></PRE>
<P>This would generate the following output:
<BR>
<PRE>
<FONT COLOR="#000080">1 main [greeting.c]
2 my_print [greeting.c]
3 printf
4 my_print2 [greeting.c]
5 strlen
6 malloc
7 printf</FONT></PRE>
<P>calls recognizes a number of command-line options that enable you to specify the appearance of the output and what function calls get displayed. For more information on these command-line options, refer to the calls manual page or type calls -h at the
command line.
<BR>
<BR>
<A NAME="E69E341"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>cproto</B></FONT></CENTER></H4>
<BR>
<P>cproto is a program included on this book's CD-ROM. cproto reads in C source files and automatically generates function prototypes for all of the functions. Using cproto saves you from having to type in a function definition for all of the functions
that you have written in your programs.
<BR>
<P>If you ran the following code through the cproto program
<BR>
<PRE>
<FONT COLOR="#000080">#include <stdio.h>
void main ()
{
char my_string[] = "hello there";
my_print (my_string);
my_print2(my_string);
}
void my_print (char *string)
{
printf ("The string is %s\n", *string);
}
void my_print2 (char *string)
{
char *string2;
int size, size2, i;
size = strlen (string);
size2 = size -1;
string2 = (char *) malloc (size + 1);
for (i = 0; i < size; i++)
string2[size2 - i] = string[i];
string2[size] = '\0';
printf ("The string printed backward is %s\n", string2);
}</FONT></PRE>
<P>you would get the following output:
<BR>
<PRE>
<FONT COLOR="#000080">/* greeting.c */
void main(void);
void my_print(char *string);
void my_print2(char *string);</FONT></PRE>
<P>This output could be redirected to an include file and used to define the prototypes for all of the functions.
<BR>
<BR>
<A NAME="E69E342"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>indent</B></FONT></CENTER></H4>
<BR>
<P>The indent utility is another programming utility that is included with Linux. This program, in its simplest form, reformats or pretty prints your C code so that it is consistently indented and all opening and closing braces are represented
consistently. There are numerous options that enable you to specify how you want indent to format your code. For information on these options, refer to the indent manual page or type indent -h at the command line.
<BR>
<P>The following example shows the default output of the indent program.
<BR>
<P>C code before running indent:
<BR>
<PRE>
<FONT COLOR="#000080">#include <stdio.h>
void main () {
void my_print(char *);
void my_print2(char *);
char my_string[] = "hello there";
my_print (my_string);
my_print2(my_string); }
void my_print (char *string)
{
printf ("The string is %s\n", *string);
}
void my_print2 (char *string) {
char *string2;
int size, size2, i;
size = strlen (string);
size2 = size -1;
string2 = (char *) malloc (size + 1);
for (i = 0; i < size; i++)
string2[size2 - i] = string[i];
string2[size] = '\0';
printf ("The string printed backward is %s\n", string2);
}</FONT></PRE>
<P>C code after running indent:
<BR>
<PRE>
<FONT COLOR="#000080">#include <stdio.h>
void main ()
{
void my_print(char *);
void my_print2(char *);
char my_string[] = "hello there";
my_print (my_string);
my_print2 (my_string);
}
void my_print (char *string)
{
printf ("The string is %s\n", *string);
}
void my_print2 (char *string)
{
char *string2;
int size, size2, i;
size = strlen (string);
size2 = size -1;
string2 = (char *) malloc (size + 1);
for (i = 0; i < size; i++)
string2[size2 - i] = string[i];
string2[size] = '\0';
printf ("The string printed backward is %s\n", string2);
}</FONT></PRE>
<P>Indent does not change how the code compiles; it just changes how the source code looks. It makes the code more readable, which is always a good thing.
<BR>
<BR>
<A NAME="E69E343"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>gprof</B></FONT></CENTER></H4>
<BR>
<P>gprof is a program that is installed in the /usr/bin directory on your Linux system. It allows you to profile C, Pascal, or Fortran programs to determine where most of the execution time is being spent.
<BR>
<P>gprof will tell you how many times each function used by your program is called, and also the percentage of the total execution time the program spent on each function. This information can be very useful if you are trying to improve the performance of
a program.
<BR>
<P>To use gprof on one of your C programs, you must compile the program using gcc's -pg option. This causes the program to create a file called gmon.out each time it is executed. gprof uses the gmon.out file to generate the profile information.
<BR>
<P>After you run your program and it has created the gmon.out file, you can get its profile by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gprof <program_name></FONT></PRE>
<P>The program_name parameter is the name of the program that created the gmon.out file.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The profile data that gprof displays to the screen is quite large. If you want to examine this data, you should either redirect gprof's output to a file or use the more or less pipes.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E344"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>f2c and p2c</B></FONT></CENTER></H4>
<BR>
<P>f2c and p2c are two source code conversion programs. f2c converts FORTRAN77 code into either C or C++ code, and p2c converts Pascal code into C code. Both are included in the Linux installation when you install GCC.
<BR>
<P>If you have some code that has been written using either FORTRAN77 or Pascal that you want to rewrite in C, f2c and p2c can prove to be very useful programs. Both programs produce C code that can typically be compiled directly by gcc without any human
intervention.
<BR>
<P>If you are converting small, straightforward FORTRAN77 or Pascal programs, you should be able to get away with using f2c or p2c without any options. If you are converting very large programs consisting of many files, you will probably have to use some
of the command-line options that are provided by the conversion program that you are using.
<BR>
<P>To invoke f2c on a FORTRAN program, enter the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">f2c my_fortranprog.f</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>f2c requires that the program being converted has either a .f or a .F extension.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>To convert a Pascal program to C, enter the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">p2c my_pascalprogram.pas</FONT></PRE>
<P>Both of these commands create C source code files that have the same name as the original file, except with a .c extension instead of .f or .pas.
<BR>
<P>For more information on the specific conversion options that are available with f2c or p2c, refer to their respective man pages.
<BR>
<BR>
<A NAME="E68E225"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Summary</B></FONT></CENTER></H3>
<BR>
<P>This chapter introduced the GNU C compiler and many of the options that you will typically use when you compile C code. It also introduced the concepts behind debugging code with the GNU debugger, and illustrated the usefulness of some of the other C
utility programs that are either included on the Linux CD-ROM, or available via FTP from sunsite.unc.edu.
<BR>
<P>If you will be writing C code, the time that you spend learning how to use gdb and some of the other tools mentioned in this chapter will be more than worth the eventual time-saving that you will gain.
<BR>
<P>The next chapter will discuss many of the same topics, but with a focus on C++ development rather than C development.
<P ALIGN=LEFT>
<A HREF="rhl26.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl26.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl28.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl28.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
</BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?