日期:2014-05-16  浏览次数:20799 次

Linux 系统下 C 语言程序设计

Linux 系统下 C 语言程序设计
2011年10月04日
   目前 Linux 已经被广泛的使用,因此有必要简单介绍一下,在 Linux 系统下如何进行 C
  语言程序设计。首先介绍在 Linux 下如何编辑 C 语言源程序,接下来介绍如何编译 C 语言
  源程序,
  最好介绍如何调试与运行 C 语言源程序。
  本节用一个简单的实例讲解应用 gdb 调试程序的基本步骤。
  C 程序设计实验指导
  首先利用 vi 编辑建立名为 test.c 的程序文件。命令如下:
  $ vi test.c
  此时在当前的目录下建立名为 test.c 的文本文件。接下来,在 vi 编辑环境中使用i命令
  输入如下的程序代码:
  #include "stdio.h"
  #include "string.h"
  void GetTitle(char *pszText)
  {
  char szText[]="This is a Test C Program!\n";
  strcpy(pszText,szText);
  }
  void PrintTitle(char *pszText)
  {
  unsigned int n,i;
  char a;
  n=strlen(pszText);
  for(i=0;ic",a);
  }
  }
  int main()
  {
  int a;
  char szTitle[255];
  a=20;
  GetTitle(szTitle);
  PrintTitle(szTitle);
  printf("A=%d",a);
  return 0;
  }
  在程序编写完成之后,vi 编辑环境中按下 Esc 键,并输入:wq 命令,将当前的修改存盘
  并退出 vi 编辑环境。
  用下面的命令编译 test.c,以便生成调试信息,准备调试。执行如下的命令
  $gcc -g -o test test.c
  启动 gdb 并加载 test
  $gdb test
  系统将显示如下的信息
  GNU gdb 19991004
  Copyright 1998 Free Software Foundation, Inc.
  GDB is free software, covered by the GNU General Public License, and you are
  welcome to change it and/or distribute copies of it under certain conditions.
  Type "show copying" to see the conditions.
  -8-
  集成开发环境的简介
  There is absolutely no warranty for GDB. Type "show warranty" for details.
  This GDB was configured as "i386-redhat-linux"...
  首先运行一下程序,大致看一下结果:
  (gdb) run
  系统将显示如下的信息
  Starting program: /a.out
  This is a Test C Program!
  A=20
  Program exited normally.
  接下来执行 list 命令,查看程序的代码,主要是查看代码的行号。
  (gdb) list 0
  系统将显示如下的信息
  1
  #include "stdio.h"
  2
  #include "string.h"
  3
  void GetTitle(char *pszText)
  4
  {
  5
  char szText[]="This is a Test C Program!\n";
  6
  strcpy(pszText,szText);
  7
  }
  8
  void PrintTitle(char *pszText)
  9
  {
  unsigned int n,i;
  由于屏幕有限,因此仅仅显示了 10 行代码,查看下面的 10 行代码,在此输入 list 命令
  即可。
  (gdb) list
  系统将显示如下的信息
  11
  char a;
  12
  n=strlen(pszText);
  13
  for(i=0;ic",a);
  17
  }
  18 }
  19 int main()
  {
  接下来,再次输入 list 命令,显示剩余的代码。
  (gdb) list
  系统将显示如下的信息:
  21 int a;
  22 char szTitle[255];
  23 a=20;
  24 GetTitle(szTitle);
  25 PrintTitle(szTitle);
  版权所有:东北大学计算中心
  -9-
  C 程序设计实验指导
  26
  27
  }
  printf("A=%d",a);
  return 0;
  为了查看在程序在 PrintTitle 函数中的循环体的运行情况,将断点设置在 15 行,以便观
  察变量 a 的变化。因此输入命令
  (gdb) break 15
  系统将显示如下的信息
  Breakpoint 1 at 0x80484a0: file test.c, line 15.
  接下来使用 run 命令运行程序,
  (gdb) run
  系统将显示如下的信息
  Starting program: /a.out
  Breakpoint 1, PrintTitle (pszText=0xbffff8b4 "This is a Test C Program!\n")
  at test.c:15
  a=pszText[i];
  提示信息说明程序运行到断点位置停了下来,此时可以通过 watch 命令查看变量的内
  容。下面查看变量 a 的内容的变化情况,输入如下的命令:
  (gdb) watch a
  系统将显示如下的设置成功信息
  Hardware watchpoint 2: a
  输入下面的指令,运行下一步。
  (gdb) next
  系统将显示变量 a 的变化情况
  Hardware watchpoint 2: a
  Old value = 111 'o'
  New value = 84 'T'
  PrintTitle (pszText=0xbffff8b4 "This is a Test C Program!\n") at test.c:16
  printf("%c",a);
  再次执行 next 命令查看下一步的结果。
  (gdb) next
  系统将显示如下信息
  #0
  16
  13
  PrintTitle (pszText=0xbffff8b4 "This is a Test C Program!\n") at test.c:16
  printf("%c",a);
  for(i=0;iC Program!\n") at test.c:13
  for(i=0;iC Program!\n")