📄 ch27.htm
字号:
fixes, or revert to an older compiler version.
<HR>
</DL>
<P><TT>calls</TT> 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 <TT>calls</TT> manual page
or type <TT>calls -h</TT> at the command line.
<CENTER>
<H4><A NAME="Heading27<FONT COLOR="#000077">cproto</FONT></H4>
</CENTER>
<P><TT>cproto</TT> is another program that is not included on this Linux CD-ROM but
is readily available from FTP and BBS sites. <TT>cproto</TT> reads in C source files
and automatically generates function prototypes for all of the functions. Using <TT>cproto</TT>
saves you from having to type in a function definition for all of the functions that
you have written in your programs.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading28<FONT COLOR="#000077"><B>NOTE: </B></FONT>To install <TT>cproto</TT>
on your system, perform the following steps while you are logged in as root: 1. Uncompress
and <TT>untar</TT> the file. 2. <TT>cd</TT> into the <TT>cproto</TT> subdirectory
that was created by <TT>untar</TT>ring the file. 3. Move the file named <TT>cproto</TT>
to the <TT>/usr/bin</TT> directory. 4. Move the file named <TT>cproto.1</TT> to the
<TT>/usr/man/man1</TT> directory. 5. Remove the <TT>/tmp/cproto</TT> directory. This
will install the <TT>cproto</TT> program and man page on your system.
<HR>
</DL>
<P>If you ran the following code through the <TT>cproto</TT> program</P>
<PRE><FONT COLOR="#0066FF">#include <stdio.h>
main ()
{
char my_string[] = "hello there";
my_print (my_string);
my_print2(my_string);
}
my_print (char *string)
{
printf ("The string is %s\n", *string);
}
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:</P>
<PRE><FONT COLOR="#0066FF">/* test.c */
int main(void);
int my_print(char *string);
int my_print2(char *string);
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading29<FONT COLOR="#000077"><B>WARNING:</B> </FONT>As with the
<TT>calls</TT> program, discussed earlier, the version of <TT>gcc</TT> included on
this book's CD-ROM may cause errors when compiling this program. You should obtain
a bug fix (if one is available) or revert to an earlier version of the compiler to
avoid these problems.
<HR>
</DL>
<P>This output could be redirected to an include file and used to define the prototypes
for all of the functions.
<CENTER>
<H4><A NAME="Heading30<FONT COLOR="#000077">indent</FONT></H4>
</CENTER>
<P>The <TT>indent</TT> 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
<TT>indent</TT> to format your code. For information on these options, refer to the
<TT>indent</TT> manual page or type <TT>indent -h</TT> at the command line.</P>
<P>The following example shows the default output of the <TT>indent</TT> program.</P>
<P>C code before running <TT>indent</TT>:</P>
<PRE><FONT COLOR="#0066FF">#include <stdio.h>
main () {
char my_string[] = "hello there";
my_print (my_string);
my_print2(my_string); }
my_print (char *string)
{
printf ("The string is %s\n", *string);
}
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 <TT>indent</TT>:</P>
<PRE><FONT COLOR="#0066FF">#include <stdio.h>
main ()
{
char my_string[] = "hello there";
my_print (my_string);
my_print2 (my_string);
}
my_print (char *string)
{
printf ("The string is %s\n", *string);
}
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><TT>Indent</TT> 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.
<CENTER>
<H4><A NAME="Heading31<FONT COLOR="#000077">gprof</FONT></H4>
</CENTER>
<P><TT>gprof</TT> is a program that is installed in the <TT>/usr/bin</TT> directory
on your Linux system. It allows you to profile programs that you write to determine
where most of the execution time is being spent.</P>
<P><TT>gprof</TT> will tell you how many times each function that your program uses
is called, and also the percentage of the total execution time the program spent
in each function. This information can be very useful if you are trying to improve
the performance of a program.</P>
<P>To use <TT>gprof</TT> on one of your programs, you must compile the program using
the <TT>-pg gcc</TT> option. This causes the program to create a file called <TT>gmon.out</TT>
each time it is executed. <TT>gprof</TT> uses the <TT>gmon.out</TT> file to generate
the profile information.</P>
<P>After you run your program and it has created the <TT>gmon.out</TT> file, you
can get its profile by entering the following command:</P>
<PRE><FONT COLOR="#0066FF">gprof <program_name>
</FONT></PRE>
<P>The <TT>program_name</TT> parameter is the name of the program that created the
<TT>gmon.out</TT> file.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading32<FONT COLOR="#000077"><B>TIP:</B> </FONT>The profile data
that <TT>gprof</TT> displays to the screen is quite large. If you want to examine
this data, you should redirect <TT>gprof</TT>'s output to a file.
<HR>
</DL>
<CENTER>
<H4><A NAME="Heading33<FONT COLOR="#000077">f2c and p2c</FONT></H4>
</CENTER>
<P><TT>f2c</TT> and <TT>p2c</TT> are two source code conversion programs. <TT>f2c</TT>
converts FORTRAN code into C code, and <TT>p2c</TT> converts Pascal code into C code.
Both are included in the Linux installation when you install GCC.</P>
<P>If you have some code that has been written using either FORTRAN or Pascal that
you want to rewrite in C, <TT>f2c</TT> and <TT>p2c</TT> can prove to be very useful
programs. Both programs produce C code that can typically be compiled directly by
GCC without any human intervention.</P>
<P>If you are converting small, straightforward FORTRAN or Pascal programs, you should
be able to get away with using <TT>f2c</TT> or <TT>p2c</TT> 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.</P>
<P>To invoke <TT>f2c</TT> on a FORTRAN program, enter the following command:</P>
<PRE><FONT COLOR="#0066FF">f2c my_fortranprog.f
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading34<FONT COLOR="#000077"><B>NOTE:</B> </FONT><TT>f2c</TT> requires
that the program being converted has either a <TT>.f</TT> or a <TT>.F</TT> extension.
<HR>
</DL>
<P>To convert a Pascal program to C, enter the following command:</P>
<PRE><FONT COLOR="#0066FF">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 <TT>.c</TT> extension instead of <TT>.f</TT> or <TT>.pas</TT>.</P>
<P>For more information on the specific conversion options that are available with
<TT>f2c</TT> or <TT>p2c</TT>, refer to their respective man pages.
<CENTER>
<H3><A NAME="Heading35<FONT COLOR="#000077">Summary</FONT></H3>
</CENTER>
<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 included on the Linux CD-ROM.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading36<FONT COLOR="#000077"><B>TIP:</B> </FONT>If you will be writing
C code, the time that you spend learning how to use <TT>gdb</TT> and some of the
other tools mentioned in this chapter will be more than worth your effort in light
of the time you will save later.
<HR>
</DL>
<P>The next chapter will discuss many of the same topics, but with a focus on C++
development rather than C development.
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -