文档全是精心选出来的精品资料,绝对值得下载!
实验名称:Linux下C编程
使用gcc调试如下程序,按要求完成任务
任务1----单个程序reverse.c
#include
int reverse();
main()
{
char str[100];
reverse("cat",str);
printf("reverse(cat)=%s\n",str);
reverse("noon",str);
printf("reverse(noon)=%s\n",str);
}
int reverse(before,after)
char *before;
char *after;
{
int i,j,len;
len=strlen(before);
for(j=len-1,i=0;j>=0;j--,i++)
after[i]=before[j];
after[len]=0;
}
任务2----多模块程序
对reverse.c程序进行处理,将reverse()函数从原文件中移除,创建可重用函数。
步骤:
创建一个原代码模块,其中包含函数的原代码和包含函数原型的头文件;
main.c函数:
#include
#include “reverse.h”
main()
{
char str[100];
reverse("cat",str);
printf("reverse(cat)=%s\n",str);
reverse("noon",str);
printf("reverse(noon)=%s\n",str);
}
reverse-m.c函数:
int reverse(char *before, char *after)
{
int i,j,len;
len=strlen(before);
for(j=len-1;i=0;j>=0;j--;i++)
after[i]=before[j];
after[len]=0;
}
reverse.h头文件:
int reverse(char *,char*);
用gcc的-c选项把模块编译为目标模块。
gcc –c main.c reverse-m.c
连接编译住程序和目标模块生成可执行文件。
gcc –o program main.o reverse-m.o
任务3----使用reverse函数构建回文程序palind,调试执行。
包括:palind头文件、palind函数文件代码、palind主文件。
提示:
palind头文件:
int palind();
Palind-m.c模块文件:
#include “palind.h”
#include “reverse.h”
#include
int palind(str)
char *str;
{
char reversedStr[100];
reverse(str,reversedStr);
return(strcmp(str,reversedStr)==0);
}
palind.c主程序代码:
#include
#include “palind.h”
main()
{
printf(“palind(‘cat’)=%d\n”,palind(“cat”));
printf(“palind(‘noon’)=%d\n”,palind(“noon”));
}
编译模块、连接生成可执行文件
编译模块
gcc -c palind-m.c palind.c
连接生成可执行文件
gcc reverse-m.o palind-m.o palind.o -o palind
任务4----尝试创建归档文件palind.a ,包括所有与回文有关的目标模块
ar crv palind.a reverse-m.o palind-m.o palind.o
2.调试课件关于gdb的例题。
源程序test.c:
#include#include
static char buff [256];
static char* string;
int main()
{
printf(“please ingput a string:”);
gets (string);
printf(“\n your string is :%s\n”,string);
}
调试:
gcc –o test test.c
gcc –o test –g test.c
gdb test
(gdb) run
(gdb) list(查看代码)