新闻中心

EEPW首页>嵌入式系统>设计应用> Linux下C应用程序开发

Linux下C应用程序开发

作者: 时间:2011-05-20 来源:网络 收藏
另外的 C 编程工具
  xxgdb
  xxgdb 是 gdb 的一个基于 X Window 系统的图形界面. xxgdb 包括了命令行版的 gdb 上的所有特性. xxgdb 使你能通过按按钮来执行常用的命令. 设置了断点的地方也用图形来显示.
你能在一个 Xterm 窗口里键入下面的命令来运行它:
  xxgdb
  你能用 gdb 里任何有效的命令行选项来初始化 xxgdb . 此外 xxgdb 也有一些特有的命令行选项, 表 27.2 列出了这些选项.
  表 27.2. xxgdb 命令行选项.
  选 项 描 述
  db_name 指定所用调试器的名字, 缺省是 gdb.
  db_prompt 指定调试器提示符, 缺省为 gdb.
  gdbinit 指定初始化 gdb 的命令文件的文件名, 缺省为 .gdbinit.
  nx 告诉 xxgdb 不执行 .gdbinit 文件.
  bigicon 使用大图标.
  calls
  你可以在 sunsite.unc.edu FTP 站点用下面的路径:
  /pub/ /devel/lang/c/calls.tar.Z
  来取得 calls , 一些旧版本的 CD-ROM 发行版里也附带有. 因为它是一个有用的工具, 我们在这里也介绍一下. 如果你觉得有用的话, 从 BBS, FTP, 或另一张CD-ROM 上弄一个拷贝. calls 调用 GCC 的预处理器来处理给出的源程序文件, 然后输出这些文件的里的函数调用树图.
  注意: 在你的系统上安装 calls , 以超级用户身份登录后执行下面的步骤: 1. 解压和 untar   文件. 2. cd 进入 calls untar 后建立的子目录. 3. 把名叫 calls 的文件移动到 /usr/bin 目录. 4. 把名叫 calls.1 的文件移动到目录 /usr/man/man1 . 5. 删除 /tmp/calls 目录. 这些步骤将把 calls 程序和它的指南页安装载你的系统上.
------------------------------------------------------------------------------
--
  当 calls 打印出调用跟踪结果时, 它在函数后面用中括号给出了函数所在文件的文件名:
main [hello.c]
  如果函数并不是向 calls 给出的文件里的, calls 不知道所调用的函数来自哪里, 则只显示函数的名字:
  printf
  calls 不对递归和静态函数输出. 递归函数显示成下面的样子:
  fact recursive in factorial.c >;>;>;
  静态函数象这样显示:
  total [static in calculate.c]
  作为一个例子, 假设用 calls 处理下面的程序:
  #include stdio.h>;
  static void my_print (char *);
  static void my_print2 (char *);
  main ()
  {
  char my_string[] = hello world!;
  my_print (my_string);
  my_print2 (my_string);
  my_print (my_string);
  }
  void count_sum()
  {
  int i,sum=0;
  for(i=0; i1000000; i++)
  sum += i;
  }
  void my_print (char *string)
  {
  count_sum();
  (The string is %s , string);
  }
  void my_print2 (char *string)
  {
  char *string2;
  int size, i,sum =0;
  printf (The string printed backward is %s , string2);
  }
  将产生如下的输出:
  1 __underflow [hello.c]
  2 main
  3 my_print [hello.c]
  4 count_sum [hello.c]
  5 printf
  6 my_print2 [hello.c]
  7 count_sum
  8 strlen
  9 malloc
  10 printf
  calls 有很多命令行选项来设置不同的输出格式, 有关这些选项的更多信息请参考 calls 的指南页. 方法是在命令行上键入 calls -h .
  calltree
  calltree与calls类似,初了输出函数调用树图外,还有其它详细的信息。可以从sunsite.unc.edu FTP 站点用下面的路径
  :/pub/ /devel/lang/c/calltree.tar.gz得到calltree.
  cproto
  cproto 读入 C 源程序文件并自动为每个函数产生原型申明. 用 cproto 可以在写程序时为你节省大量用来定义函数原型的时间.
  如果你让 cproto 处理下面的代码(cproto hello.c):
  #include stdio.h>;
  static void my_print (char *);
  static void my_print2 (char *);
  main ()
  {
  char my_string[] = hello world!;
  my_print (my_string);
  my_print2 (my_string);
  }
  void my_print (char *string)
  {
  printf (The string is %s , string);
  }
  void my_print2 (char *string)
  {
  char *string2;
  int size, i;
  size = strlen (string);
  string2 = (char *) malloc (size + 1);
  for (i = 0; i size; i++)
  string2[size -1 - i] = string;
  string2[size] = '';
  printf (The string printed backward is %s , string2);
  }
  你将得到下面的输出:
  /* hello.c */
  int main(void);
  int my_print(char *string);
  int my_print2(char *string);
  这个输出可以重定向到一个定义函数原型的包含文件里.
  indent
  indent 实用程序是 Linux 里包含的另一个编程实用工具. 这个工具简单的说就为你的代码产生美观的缩进的格式. indent 也有很多选项来指定如何格式化你的源代码.这些选项的更多信息请看indent 的指南页, 在命令行上键入 indent -h .
  下面的例子是 indent 的缺省输出:
  运行 indent 以前的 C 代码:
  #include stdio.h>;
  static void my_print (char *);
  static void my_print2 (char *);
  main ()
  {
  char my_string[] = hello world!;
  my_print (my_string);
  my_print2 (my_string);
  }
  void my_print (char *string)
  {
  printf (The string is %s , string);
  }
  void my_print2 (char *string)
  {
  char *string2; int size, i;
  size = strlen (string);
  string2 = (char *) malloc (size + 1);
  for (i = 0; i size; i++) string2[size -1 - i] = string;
  string2[size] = '';
  printf (The string printed backward is %s , string2);
  }
  运行 indent 后的 C 代码:
  #include stdio.h>;
  static void my_print (char *);
  static void my_print2 (char *);
  main ()
  {
  char my_string[] = hello world!;
  my_print (my_string);
  my_print2 (my_string);
  }
  void
  my_print (char *string)
  {
  printf (The string is %s , string);
  }
  void
  my_print2 (char *string)
  {
  char *string2;
  int size, i;
  size = strlen (string);
  string2 = (char *) malloc (size + 1);
  for (i = 0; i size; i++)
  string2[size - 1 - i] = string;
  string2[size] = '';
  printf (The string printed backward is %s , string2);
  }
  indent 并不改变代码的实质内容, 而只是改变代码的外观. 使它变得更可读, 这永远是一件好事.
  gprof
  gprof 是安装在你的 Linux 系统的 /usr/bin 目录下的一个程序. 它使你能剖析你的程序从而知道程序的哪一个部分在执行时最费时间.
  gprof 将告诉你程序里每个函数被调用的次数和每个函数执行时所占时间的百分比. 你如果想提高你的程序性能的话这些信息非常有用.
  为了在你的程序上使用 gprof, 你必须在编译程序时加上 -pg 选项. 这将使程序在每次执行时产生一个叫 gmon.out 的文件. gprof 用这个文件产生剖析信息.
  在你运行了你的程序并产生了 gmon.out 文件后你能用下面的命令获得剖析信息:
  gprof program_name>;
  参数 program_name 是产生 gmon.out 文件的程序的名字.
  为了说明问题,在程序中增加了函数count_sum()以消耗CPU时间,程序如下
  #include stdio.h>;
  static void my_print (char *);
  static void my_print2 (char *);
  main ()
  {
  char my_string[] = hello world!;
  my_print (my_string);
  my_print2 (my_string);
  my_print (my_string);
  }
  void count_sum()
  {
  int i,sum=0;
  for(i=0; i1000000; i++)
  sum += i;
  }
  void my_print (char *string)
  {
  count_sum();
  printf (The string is %s , string);
  }
  void my_print2 (char *string)
  {
  char *string2;
  int size, i,sum =0;
  count_sum();
  size = strlen (string);
  string2 = (char *) malloc (size + 1);
  for (i = 0; i size; i++) string2[size -1 - i] = string;
  string2[size] = '';
  for(i=0; i5000000; i++)
  sum += i;
  printf (The string printed backward is %s , string2);
  }
  $ gcc -pg -o hello hello.c
  $ ./hello
  $ gprof hello | more
  将产生以下的输出
  Flat profile:
  Each sample counts as 0.01 seconds.
  % cumulative self self total
  time seconds seconds calls us/call us/call name
  69.23 0.09 0.09 1 90000.00 103333.33 my_print2
  30.77 0.13 0.04 3 13333.33 13333.33 count_sum
  0.00 0.13 0.00 2 0.00 13333.33 my_print
  % 执行此函数所占用的时间占程序总
  time 执行时间的百分比
  cumulative 累计秒数 执行此函数花费的时间
  seconds (包括此函数调用其它函数花费的时间)
  self 执行此函数花费的时间
  seconds (调用其它函数花费的时间不计算在内)
  calls 调用次数
  self 每此执行此函数花费的微秒时间
  us/call
  total 每此执行此函数加上它调用其它函数
  us/call 花费的微秒时间
  name 函数名
  由以上数据可以看出,执行my_print()函数本身没花费什么时间,但是它又调用了
count_sum()函数,所以累计秒数为0.13.
  技巧: gprof 产生的剖析数据很大, 如果你想检查这些数据的话最好把输出重定向到一个文件里. linux操作系统文章专题:linux操作系统详解(linux不再难懂)

linux相关文章:linux教程



上一页 1 2 3 下一页

评论


相关推荐

技术专区

关闭