478-482.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 214 行

HTML
214
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Programming in C</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=26//-->

<!--PAGES=478-482//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="474-478.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="482-484.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">cproto</FONT></H4>

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

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>To install <TT>cproto</TT> on your system, perform the following steps while you are logged in as <TT>root</TT>:

<DL>

<DD><B>1.</B>&nbsp;&nbsp;Uncompress and <TT>untar</TT> the file.

<DD><B>2.</B>&nbsp;&nbsp;<TT>cd</TT> into the <TT>cproto</TT> subdirectory created as a result of the <TT>untar</TT> command.

<DD><B>3.</B>&nbsp;&nbsp;Move the file named <TT>cproto</TT> to the <TT>/usr/bin</TT> directory.

<DD><B>4.</B>&nbsp;&nbsp;Move the file named <TT>cproto.1</TT> to the <TT>/usr/man/man1</TT> directory.

<DD><B>5.</B>&nbsp;&nbsp;Remove the <TT>/tmp/cproto</TT> directory.

</DL>

<P>This installs the <TT>cproto</TT> program and man page on your system.<HR></FONT>

</BLOCKQUOTE>

</P>

<P>Run the following code through the <TT>cproto</TT> program:</P>

<!-- CODE //-->

<PRE>

#include &lt;stdio.h&gt;



main ()

&#123;

  char my_string[] = &#147;hello there&#148;;

  my_print (my_string);

  my_print2(my_string);

&#125;



my_print (char *string)

&#123;

  printf (&#147;The string is %s\n&#148;, *string);

&#125;



my_print2 (char *string)

&#123;

  char *string2;

  int size, size2, i;



  size = strlen (string);

  size2 = size -1;

  string2 = (char *) malloc (size &#43; 1);

  for (i = 0; i &lt; size; i&#43;&#43;)

    string2[size2 - i] = string[i];

  string2[size] = &#146;\0&#146;;

  printf (&#147;The string printed backward is %s\n&#148;, string2);

&#125;

</PRE>

<!-- END CODE //-->

<P>The following output displays:

</P>

<!-- CODE SNIP //-->

<PRE>

/* test.c */

int main(void);

int my_print(char *string);

int my_print2(char *string);

</PRE>

<!-- END CODE SNIP //-->

<P>This output could be redirected to an include file and used to define the prototypes for all of the functions.

</P>

<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">indent</FONT></H4>

<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> man 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>

<!-- CODE //-->

<PRE>

#include &lt;stdio.h&gt;



main () &#123;

      char my_string[] = &#147;hello there&#148;;

  my_print (my_string);

    my_print2(my_string); &#125;



my_print (char *string)

&#123;

  printf  (&#147;The string is %s\n&#148;, *string);

&#125;



my_print2      (char *string) &#123;

    char *string2;

      int size, size2, i;



      size = strlen (string);

      size2 = size -1;

      string2 = (char *) malloc (size &#43; 1);

  for (i = 0; i &lt; size; i&#43;&#43;)

           string2[size2 - i] = string[i];

      string2[size] = &#146;\0&#146;;

      printf (&#147;The string printed backward is %s\n&#148;, string2);

&#125;

</PRE>

<!-- END CODE //-->

<P>C code after running <TT>indent</TT>:</P>

<!-- CODE //-->

<PRE>

#include &lt;stdio.h&gt;



main ()

&#123;

  char my_string[] = &#147;hello there&#148;;

  my_print (my_string);

  my_print2 (my_string);

&#125;



my_print (char *string)

&#123;

  printf (&#147;The string is %s\n&#148;, *string);

&#125;



my_print2 (char *string)

&#123;

  char *string2;

  int size, size2, i;



  size = strlen (string);

  size2 = size -1;

  string2 = (char *) malloc (size &#43; 1);

  for (i = 0; i &lt; size; i&#43;&#43;)

    string2[size2 - i] = string[i];

  string2[size] = &#146;\0&#146;;

  printf (&#147;The string printed backward is %s\n&#148;, string2);

&#125;

</PRE>

<!-- END CODE //-->

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

<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">gprof</FONT></H4>

<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>

<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>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>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR>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>&#146;s output to a file.<HR></FONT>

</BLOCKQUOTE>

<!-- CODE SNIP //-->

<PRE>

gprof &lt;program_name&gt;

</PRE>

<!-- END CODE SNIP //-->

<P>The <TT>program_name</TT> parameter is the name of the program that created the <TT>gmon.out</TT> file.</P>

<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">f2c and p2c</FONT></H4>

<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 which are provided by the conversion program that you are using.</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Tip:&nbsp;&nbsp;</B><BR><TT>f2c</TT> requires that the program being converted has either a <TT>.f</TT> or a <TT>.F</TT> extension.<HR></FONT>

</BLOCKQUOTE>

<P>To invoke <TT>f2c</TT> on a FORTRAN program, enter the following command:</P>

<!-- CODE SNIP //-->

<PRE>

f2c my_fortranprog.f

</PRE>

<!-- END CODE SNIP //-->

<P>To convert a Pascal program to C, enter the following command:

</P>

<!-- CODE SNIP //-->

<PRE>

p2c my_pascalprogram.pas

</PRE>

<!-- END CODE SNIP //-->

<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.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="474-478.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="482-484.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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