教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 实用模板 >

Red hat Linux 实验简易教程 - 1.1 - 图文(8)

来源:网络收集 时间:2026-04-27
导读: 第五章 常用开发工具 一.实验目的 1.掌握C语言编译的基本用法 2.掌握gdb调试工具的基本用法 3.理解make工具的功能,学会编制makefile 的方法 二.实验内容 1.利用gcc编译C语言程序,使用不同选项,观察并分析显示

第五章 常用开发工具

一.实验目的

1.掌握C语言编译的基本用法 2.掌握gdb调试工具的基本用法

3.理解make工具的功能,学会编制makefile 的方法 二.实验内容

1.利用gcc编译C语言程序,使用不同选项,观察并分析显示结果(随意编写一C文件cc.c) 2.利用gdb调试一个编译后的C语言程序(自己编写C文件sort.c)

3.编写一个由多文件构成的C语言程序,编制makefile,运行make工具进行维护。 三、主要实验步骤

1.利用gcc编译C语言程序,使用不同选项,观察并分析显示结果(随意编写一C文件cc.c) $gcc cc.c //直接编译源文件cc.c $./a.out //运行可执行文件 a.out $gcc cc.c -o exe //-o 指定生成文件名 $./exe //运行可执行文件exe $gcc -E cc.c -o cc.i // 预处理 $gcc cc.i -o cc $./cc

2.利用gdb调试一个编译后的C语言程序(自己编写C文件sort.c) $gcc -g sort.c -o debug // -g 选项编译用以调试

$ gdb debug //进入调试环境 用gdb调试 debug (gdb)list 1,30 // 显示源码 (gdb)break 12 //增加断点 (gdb)run //运行到断点

(gdb)step //单步运行 回车继续... (gdb)p a[j] //显示a[j]值 ... //若干命令 (gdb)quit //退出调试环境

3.编写一个由多文件构成的C语言程序,编制makefile,运行make工具进行维护。 //文件1, bubbleSort.c void bubbleSort(int a[], int n) { }

int i,j;

for(i=0; i

for(j=0; j

if(a[j] > a[j+1])

swapdata(a+j, a+j+1);

- 30 -

//文件2,swapdata.c

void swapdata(int *a, int *b) { }

*a = *a + *b; *b = *a - *b; *a = *a - *b;

//文件3,selectSort.c

void selectSort(int a[], int n) { }

//文件4,main.c #include int main() { }

int a[]={9,1,5,11,7,3,2,8,6,4,0}; int len=sizeof(a)/sizeof(int); int i=0;

for(i=0; i

printf(\puts(\bubbleSort(a, len); for(i=0; i

printf(\putchar(10); int i,j,max;

for(i=0; i

max = i;

for(j=i+1; j

if( a[max] < a[j]) max = j; swapdata(a+max, a+i); if(max != i)

//文件5,makefile

sort.exe:main.o bubbleSort.o selectSort.o swap.o

gcc main.o bubbleSort.o selectSort.o swap.o -o sort.exe main.o:main.c

- 31 -

gcc -c main.c -o main.o

gcc -c bubbleSort.c -o bubbleSort.o gcc -c selectSort.c -o selectSort.o gcc -c swapdata.c -o swap.o

bubbleSort.o:bubbleSort.c selectSort.o:selectSort.c swap.o:swapdata.c

#make clean 命¨1令¢?执??行D以??下?内¨2容¨Y,ê?目?录?下?有?D文?件tclean也?2执??行D .PHONY:clean clean:

rm sort.exe *.o

进入以上5个文件所在目录,运行

$make //make编译以上5文件

4.完成对思考题6.5的调试 见 ./6_5 ,调试步骤见:

#include #include

int main(int argc, char**argv) { }

char *p; int i;

p = malloc(30);

strcpy(p,\printf(\if(argc == 2){ } /*free(p)*/ return 0; if(strcmp(argv[1], \ }

p[50] = 'a'; //数oy组á¨|下?表à¨a越?界? free(p); p[0]='b';//非¤?法¤?§访¤?问¨o,ê?已??经-free(p) else if(strcmp(argv[1], \5.完成对思考题6.6的调试

//源文件如下,调试步骤见:二、2

#include #include int make_key(void); int get_key_num(void); int number(void);

- 32 -

int main() { }

int make_key(void) { }

int get_key_num(void) { }

int number(void) { }

return 10;

int ret = number(); return ret;

int ret = get_key_num(); return ret;

int ret = make_key();

printf(\exit(EXIT_SUCCESS);

6.完成对思考题6.9的编制,并使用make进行维护 //文件 makefile 以写好,源文件请自行解决

prog:a.o b.o c.o d.o assmb.o

gcc a.o b.o c.o d.o assmb.o -L/home/user/lib -lm -o prog gcc -c a.c -o a.o gcc -c b.c -o b.o gcc -c c.c -o c.o gcc -c d.c -o d.o as -o aassmb.o assmb.s a.o:a.c b.o:b.c defs.h c.o:c.c d.o:d.c defs.h assmb.o:assmb.s .PHONY:clean clean:

rm prog *.o

//自己按照要求编写题目中文件后运行

$make

- 33 -

第六章、Linux环境编程

一、 实验目的

1. 理解系统调用和库函数的异同。 2. 学会用系统调用和库函数进行编程。

3. 掌握一些常用的系统调用和库函数的功能及应用。

二、 实验内容

1. 2. 3. 4.

使用系统调用对文件进行操作。 使用系统调用对进程进行操作。 使用管道机制进行I/O。

使用信号机制进行进程通信。

三、 主要实验步骤

1. 编写一个程序,把一个文件的内容复制到另一个文件上,即实现简单的copy 功能。

要求:只用open(),read(),write()和close()系统调用,程序的第一个参数是源文件,第二个参数是目标文件。 示例代码如下所示: #include #include #include #include #include #include

void file_copy(char *fname_str1, char *fname_str2) {

int fd1,fd2,nbytes; char buf[10];

if( (fd1=open(fname_str1,O_RDONLY))<0 ) { printf(\ exit(EXIT_FAILURE); } if( (fd2=open(fname_str2,O_WRONLY))<0 ) { printf(\ exit(EXIT_FAILURE); }

while( (nbytes=read(fd1,buf,10))>0 ) { if(write(fd2,buf,10)<0) { printf(\

- 34 -

…… 此处隐藏:1427字,全部文档内容请下载后查看。喜欢就下载吧 ……
Red hat Linux 实验简易教程 - 1.1 - 图文(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/520866.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)