📄 linux 下 c 语言编程1.html
字号:
<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> 程序和它的指南页安装载你的系统上.
<hr width="90%"></dd>
</dl>
当 <tt>calls</tt> 打印出调用跟踪结果时, 它在函数后面用中括号给出了函数所在文件的文件名:
<pre><font color="#0066ff">main [test.c]</font></pre>
如果函数并不是向 <tt>calls</tt> 给出的文件里的,
<tt>calls </tt>不知道所调用的函数来自哪里, 则只显示函数的名字:
<pre><font color="#0066ff">printf</font></pre>
<tt> calls</tt> 不对递归和静态函数输出. 递归函数显示成下面的样子:
<pre><font color="#0066ff">fact <<< recursive in factorial.c >>></font></pre>
静态函数象这样显示:
<pre><font color="#0066ff">total [static in calculate.c]</font></pre>
作为一个例子, 假设用 <tt>calls</tt> 处理下面的程序:
<br>
<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>
将产生如下的输出:
<pre><font color="#0066ff"> 1 main [test.c]
2 my_print [test.c]
3 printf
4 my_print2 [test.c]
5 strlen
6 malloc
7 printf</font></pre>
<tt>calls</tt> 有很多命令行选项来设置不同的输出格式, 有关这些选项的更多信息请参考
<tt>calls</tt> 的指南页. 方法是在命令行上键入 <tt>calls -h</tt> .
<br>
<br>
<h4>
<font color="#ff9900">
cproto</font></h4>
<tt> cproto</tt> 读入 C 源程序文件并自动为每个函数产生原型申明.
用 <tt>cproto</tt> 可以在写程序时为你节省大量用来定义函数原型的时间.
<br> 如果你让 <tt>cproto</tt> 处理下面的代码:
<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>
你将得到下面的输出:
<pre><font color="#0066ff">/* test.c */
int main(void);
int my_print(char *string);
int my_print2(char *string);</font></pre>
这个输出可以重定向到一个定义函数原型的包含文件里.
<h4>
<font color="#ff9900">
indent</font></h4>
<tt> indent</tt> 实用程序是 Linux 里包含的另一个编程实用工具.
这个工具简单的说就为你的代码产生美观的缩进的格式. <tt>indent</tt> 也有很多选项来指定如何格式化你的源代码.这些选项的更多信息请看<tt>indent</tt>
的指南页, 在命令行上键入 <tt>indent -h</tt> .
<br>
<p> 下面的例子是 <tt>indent </tt>的缺省输出:
</p><p> 运行 <tt>indent </tt>以前的 C 代码:
</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>
运行 <tt>indent </tt>后的 C 代码:
<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>
<tt> </tt> <tt>indent </tt>并不改变代码的实质内容,
而只是改变代码的外观. 使它变得更可读, 这永远是一件好事.
<h4>
gprof</h4>
<tt> gprof</tt> 是安装在你的 Linux 系统的 <tt>/usr/bin</tt>
目录下的一个程序. 它使你能剖析你的程序从而知道程序的哪一个部分在执行时最费时间.
<p><tt> gprof</tt> 将告诉你程序里每个函数被调用的次数和每个函数执行时所占时间的百分比.
你如果想提高你的程序性能的话这些信息非常有用.
</p><p> 为了在你的程序上使用 gprof, 你必须在编译程序时加上
-pg 选项. 这将使程序在每次执行时产生一个叫 gmon.out 的文件. gprof 用这个文件产生剖析信息.
</p><p> 在你运行了你的程序并产生了 gmon.out 文件后你能用下面的命令获得剖析信息:
</p><pre><font color="#0066ff">gprof <program_name></font></pre>
参数 program_name 是产生 gmon.out 文件的程序的名字.
<dl>
<dd>
<hr width="90%"><b>技巧:</b> gprof 产生的剖析数据很大, 如果你想检查这些数据的话最好把输出重定向到一个文件里.
<hr width="90%"></dd>
</dl>
<h4>
f2c 和 p2c</h4>
<tt> f2c</tt> 和 <tt>p2c </tt>是两个源代码转换程序. f2c
把 FORTRAN 代码转换为 C 代码, p2c 把 Pascal 代码转换为 C 代码. 当你安装
GCC 时这两个程序都会被安装上去.
<p> 如果你有一些用 FORTRAN 或 Pascal 写的代码要用 C 重写的话,
f2c 和 p2c 对你非常有用. 这两个程序产生的 C 代码一般不用修改就直接能被
GCC 编译.
</p><p> 如果要转换的 FORTRAN 或 Pascal 程序比较小的话可以直接使用
f2c 或 p2c 不用加任何选项. 如果要转换的程序比较庞大, 包含很多文件的话你可能要用到一些命令行选项.
</p><p> 在一个 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> .
<hr></dd>
</dl>
要把一个Pascal 程序装换为 C 程序, 输入下面的命令:
<pre><font color="#0066ff">p2c my_pascalprogram.pas</font></pre>
这两个程序产生的 C 源代码的文件名都和原来的文件名相同,
但扩展名由 .f 或 .pas 变为 .c.
<br>
<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 + -