⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux 下 c 语言编程1.html

📁 本文介绍了在 Linux 下能用于 C 应用程序开发和调试的工具. 本文的主旨是介绍如何在 Linux 下使用 C 编译器和其他 C 编程工具, 而非 C 语言编程的教程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<hr width="90%"><b>注意:</b> 在你的系统上安装 <tt>calls</tt> , 以超级用户身份登录后执行下面的步骤: 
1. 解压和 <tt>untar</tt> 文件. 2. <tt>cd</tt> 进入 <tt>calls</tt> untar 
后建立的子目录. 3. 把名叫 <tt>calls</tt> 的文件移动到 <tt>/usr/bin</tt> 
目录. 4. 把名叫 <tt>calls.1</tt> 的文件移动到目录 <tt>/usr/man/man1</tt> 
. 5. 删除 <tt>/tmp/calls</tt> 目录. 这些步骤将把 <tt>calls</tt> 程序和它的指南页安装载你的系统上.&nbsp; 
<hr width="90%"></dd>
</dl>
&nbsp;&nbsp;&nbsp; 当 <tt>calls</tt> 打印出调用跟踪结果时, 它在函数后面用中括号给出了函数所在文件的文件名: 
<pre><font color="#0066ff">main [test.c]</font></pre>
&nbsp;&nbsp;&nbsp; 如果函数并不是向 <tt>calls</tt> 给出的文件里的,&nbsp; 
<tt>calls </tt>不知道所调用的函数来自哪里, 则只显示函数的名字: 
<pre><font color="#0066ff">printf</font></pre>
<tt>&nbsp;&nbsp;&nbsp; calls</tt> 不对递归和静态函数输出. 递归函数显示成下面的样子: 
<pre><font color="#0066ff">fact &lt;&lt;&lt; recursive in factorial.c &gt;&gt;&gt;</font></pre>
&nbsp;&nbsp;&nbsp; 静态函数象这样显示: 
<pre><font color="#0066ff">total [static in calculate.c]</font></pre>
&nbsp;&nbsp;&nbsp; 作为一个例子, 假设用 <tt>calls</tt> 处理下面的程序: 
<br>&nbsp;
<pre><font color="#0066ff">#include &lt;stdio.h&gt;



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)

{

&nbsp; char *string2;

&nbsp; int size, size2, i;



&nbsp; size = strlen (string);

&nbsp; size2 = size -1;

&nbsp; string2 = (char *) malloc (size + 1);

&nbsp; for (i = 0; i &lt; size; i++)

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

&nbsp; string2[size] = `\0';

&nbsp; printf ("The string printed backward is %s\n", string2);

}</font></pre>
&nbsp;&nbsp;&nbsp; 将产生如下的输出: 
<pre><font color="#0066ff">&nbsp;&nbsp;&nbsp; 1 main [test.c]

&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my_print [test.c]

&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf

&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my_print2 [test.c]

&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strlen

&nbsp;&nbsp;&nbsp; 6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; malloc

&nbsp;&nbsp;&nbsp; 7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf</font></pre>
<tt>calls</tt> 有很多命令行选项来设置不同的输出格式, 有关这些选项的更多信息请参考 
<tt>calls</tt> 的指南页. 方法是在命令行上键入 <tt>calls -h</tt> . 
<br>&nbsp;
<br>&nbsp;
<h4>
<font color="#ff9900">
cproto</font></h4>
<tt>&nbsp;&nbsp;&nbsp; cproto</tt> 读入 C 源程序文件并自动为每个函数产生原型申明. 
用 <tt>cproto</tt> 可以在写程序时为你节省大量用来定义函数原型的时间. 
<br>&nbsp;&nbsp;&nbsp; 如果你让 <tt>cproto</tt> 处理下面的代码: 
<pre><font color="#0066ff">#include&nbsp; &lt;stdio.h&gt;



main ()

{

&nbsp; char my_string[] = "hello there";

&nbsp; my_print (my_string);

&nbsp; my_print2(my_string);

}



my_print (char *string)

{

&nbsp; printf ("The string is %s\n", *string);

}



my_print2 (char *string)

{

&nbsp; char *string2;

&nbsp; int size, size2, i;



&nbsp; size = strlen (string);

&nbsp; size2 = size -1;

&nbsp; string2 = (char *) malloc (size + 1);

&nbsp; for (i = 0; i &lt; size; i++)

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

&nbsp; string2[size] = `\0';

&nbsp; printf ("The string printed backward is %s\n", string2);

}</font></pre>
&nbsp;&nbsp;&nbsp; 你将得到下面的输出: 
<pre><font color="#0066ff">/* test.c */

int main(void);

int my_print(char *string);

int my_print2(char *string);</font></pre>
&nbsp;&nbsp;&nbsp; 这个输出可以重定向到一个定义函数原型的包含文件里. 
<h4>
<font color="#ff9900">
indent</font></h4>
<tt>&nbsp;&nbsp;&nbsp; indent</tt> 实用程序是 Linux 里包含的另一个编程实用工具. 
这个工具简单的说就为你的代码产生美观的缩进的格式. <tt>indent</tt> 也有很多选项来指定如何格式化你的源代码.这些选项的更多信息请看<tt>indent</tt> 
的指南页, 在命令行上键入 <tt>indent -h</tt> . 
<br>&nbsp;

<p>&nbsp;&nbsp;&nbsp; 下面的例子是 <tt>indent </tt>的缺省输出: 

</p><p>&nbsp;&nbsp;&nbsp; 运行 <tt>indent </tt>以前的 C 代码: 
</p><pre><font color="#0066ff">#include&nbsp; &lt;stdio.h&gt;



main () {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char my_string[] = "hello there";

&nbsp; my_print (my_string);

&nbsp;&nbsp;&nbsp;&nbsp; my_print2(my_string); }



my_print (char *string)

{

&nbsp; printf&nbsp;&nbsp;&nbsp; ("The string is %s\n", *string);

}



my_print2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (char *string) {

&nbsp;&nbsp;&nbsp; char *string2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int size, size2, i;



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size = strlen (string);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size2 = size -1;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string2 = (char *) malloc (size + 1);

&nbsp; for (i = 0; i &lt; size; i++)

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string2[size] = `\0';


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf ("The string printed backward is %s\n", string2);

}</font></pre>
&nbsp;&nbsp;&nbsp; 运行 <tt>indent </tt>后的 C 代码: 
<pre><font color="#0066ff">#include&nbsp; &lt;stdio.h&gt;



main ()

{

&nbsp; char my_string[] = "hello there";

&nbsp; my_print (my_string);

&nbsp; my_print2 (my_string);

}



my_print (char *string)

{

&nbsp; printf ("The string is %s\n", *string);

}



my_print2 (char *string)

{

&nbsp; char *string2;

&nbsp; int size, size2, i;



&nbsp; size = strlen (string);

&nbsp; size2 = size -1;

&nbsp; string2 = (char *) malloc (size + 1);

&nbsp; for (i = 0; i &lt; size; i++)

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

&nbsp; string2[size] = `\0';

&nbsp; printf ("The string printed backward is %s\n", string2);

}</font></pre>
<tt>&nbsp;&nbsp;&nbsp;&nbsp;</tt> <tt>indent </tt>并不改变代码的实质内容, 
而只是改变代码的外观. 使它变得更可读, 这永远是一件好事. 
<h4>
gprof</h4>
<tt>&nbsp;&nbsp;&nbsp; gprof</tt> 是安装在你的 Linux 系统的 <tt>/usr/bin</tt> 
目录下的一个程序. 它使你能剖析你的程序从而知道程序的哪一个部分在执行时最费时间. 

<p><tt>&nbsp;&nbsp;&nbsp; gprof</tt> 将告诉你程序里每个函数被调用的次数和每个函数执行时所占时间的百分比. 
你如果想提高你的程序性能的话这些信息非常有用.

</p><p>&nbsp;&nbsp;&nbsp; 为了在你的程序上使用 gprof, 你必须在编译程序时加上 
-pg 选项. 这将使程序在每次执行时产生一个叫 gmon.out 的文件. gprof 用这个文件产生剖析信息. 

</p><p>&nbsp;&nbsp;&nbsp; 在你运行了你的程序并产生了 gmon.out 文件后你能用下面的命令获得剖析信息: 
</p><pre><font color="#0066ff">gprof &lt;program_name&gt;</font></pre>
&nbsp;&nbsp;&nbsp; 参数 program_name 是产生 gmon.out 文件的程序的名字. 
<dl>
<dd>

<hr width="90%"><b>技巧:</b> gprof 产生的剖析数据很大, 如果你想检查这些数据的话最好把输出重定向到一个文件里.&nbsp; 
<hr width="90%"></dd>
</dl>

<h4>
f2c 和 p2c</h4> 
<tt>&nbsp;&nbsp;&nbsp; f2c</tt> 和 <tt>p2c </tt>是两个源代码转换程序. f2c 
把 FORTRAN 代码转换为 C 代码, p2c 把 Pascal 代码转换为 C 代码. 当你安装 
GCC 时这两个程序都会被安装上去. 

<p>&nbsp;&nbsp;&nbsp; 如果你有一些用 FORTRAN 或 Pascal 写的代码要用 C 重写的话, 
f2c 和 p2c 对你非常有用. 这两个程序产生的 C 代码一般不用修改就直接能被 
GCC 编译. 

</p><p>&nbsp;&nbsp;&nbsp; 如果要转换的 FORTRAN 或 Pascal 程序比较小的话可以直接使用 
f2c 或 p2c 不用加任何选项. 如果要转换的程序比较庞大, 包含很多文件的话你可能要用到一些命令行选项. 

</p><p>&nbsp;&nbsp;&nbsp; 在一个 FORTRAN 程序上使用 f2c , 输入下面的命令: 
</p><pre><font color="#0066ff">f2c my_fortranprog.f</font></pre>

<dl>
<dd>

<hr><b>注意:</b> <tt>f2c</tt> 要求被转换的程序的扩展名为 <tt>.f</tt> 或 
a <tt>.F</tt> .&nbsp; 
<hr></dd>
</dl>
&nbsp;&nbsp;&nbsp; 要把一个Pascal 程序装换为 C 程序, 输入下面的命令: 
<pre><font color="#0066ff">p2c my_pascalprogram.pas</font></pre>
&nbsp;&nbsp;&nbsp; 这两个程序产生的 C 源代码的文件名都和原来的文件名相同, 
但扩展名由 .f 或 .pas 变为 .c. 
<br>&nbsp;
<p>
</p><p align="center"></p>

      <p> </p></center></td>
    </tr>
  </tbody></table>
  </center>
</div>

</body></html>

⌨️ 快捷键说明

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