Linux编程环境
创始人
2024-03-29 00:07:31
0

一、实验目的

1.熟悉Linux下C语言程序设计的基本步骤

2.掌握gcc编译器的各种参数的使用方法

3.掌握gcc编译器创建函数库的方法

4.掌握gdb调试程序的方法

5.掌握多文件编译中的makefile的用法

二、实验软硬件要求

  1.CPU:P4 1.6GHz   内存:1G

  2.Windows7操作平台,Vmware虚拟机

三、实验预习

  1.Linux编辑器vi的使用

  2. GCC编译器

  3. GDB调试器

4. Make工程管理器

四、实验内容(实验步骤、测试数据等)

(一)编辑以下程序,按要求编译运行。

#include

int main(void)                      

{

  double counter;                    

  double result;

  double temp;

  for (counter = 0; counter < 4000.0 * 4000.0 * 4000.0 / 20.0 + 2030;counter += (5 - 3 +2 + 1 ) / 4)

   {

   temp=counter/1239;

   result=counter;

   }

   printf("运算结果是:%lf\n", result);

}

1.分别利用gcc的预处理,编译,汇编,链接命令生成test.i,test.s,test.o文件,分别用ls命令显示,要求有相应的命令和截图;

1-1生成文件test.c

 

1-2生成test.i

gcc -E test.c -o test.i

 

1-3生成test.s

gcc -S test.i -o test.s

 

1-4生成test.o

gcc -c test.s -o test.o

 

1-5ls展示

 

2.分别使用不同的优化选项O0~O3,进行编译生成可执行程序m0~m3,然后使用time命令统计程序的运行,如 time. /m0,比较运行时间。要求有运行截图和必要的分析。

2-1生成可执行程序

gcc -O0 -o m0 test.c

gcc -O1 -o m1 test.c

gcc -O2 -o m2 test.c

gcc -O3 -o m3 test.c

ls

 

time ./m0

time ./m1

time ./m2

time ./m3

 

(二)编写如下程序hello.h,hello.c,main.c

hello.h

   void hello(const char *name);

hello.c

   #include

   void hello(const char *name)

   { printf(“hello %s”,name);

}

main.c

   #include“hello.h”

   int main()

   {  hello(“everyone”);

      return 0;

   }

1.分别编辑以上三个源程序,并截图;

1-1生成源文件

cat >>hello.h

cat >>hello.c

cat >>main.c

 

2.利用gcc –c命令将hello.c编译成.o文件,并用ls命令查看。

gcc -c hello.c -o hello.o

 

好家伙,报错

 

两个引号的问题

ls

 

3. 利用以下命令,将.o文件创建静态库,并用ls查看。

ar  crv  libmyhello.a  hello.o

注:静态库的命名规范是以lib为前缀,紧跟静态库名,扩展名为.a

 

4.在程序中调用生成的静态库文件,运行程序查看输出结果。

gcc -o hello  main.c  -L. -lmyhello

./hello

 

5.删除库文件libmyhello.a,再运行hello程序,测试hello程序是否链接了该库文件。

rm libmyhello.a

./hello

 

6.创建动态函数库,得到动态库文件libmyhello.so,并用ls命令查看。

 gcc  –shared  -fPIC  -o  libmyhello.so  hello.o

gcc -fPIC -shared hello.c -o libmyhello.so

用自己的命令来

 

7.使用动态函数库,运行程序,观察运行结果。

gcc  -o  hello  main.c  -L.  -lmyhello

若出错,执行mv libmyhello.so  /usr/lib

 

(三)设计一程序,要求计算输入的整数的平均值,将程序分成多个文件编译

(main.c,average.c,average.h),并编写makefile文件;用make编译后改成返回最小值再编译。

//main.c

#include "average.h"

main()

{   float a,b,ave;

    float average(float a,float b);

    ave=average(a,b);

  }

//average.c

#include

float average(float a,float b)

{  float ave;

   scanf("%f,%f",&a,&b);

   ave=(a+b)/2;

 printf("average is %f\n",ave);

   return(ave);

}

//average.h

float average(float a,float b);

Makefile

#It is an example for describing  makefile

edit:main.o average.o

gcc -o edit main.o average.o

main.o: main.c average.h

gcc -c main.c

average.o:average.c average.h

gcc -c average.c

1-1生成多个文件

cat >>main.c

cat >>average.c

cat >>average.h

cat >>Makefile

 

 

这里的average.h里面要记得删除makefile部分

1-2

gcc average.c main.c -o test

./test

 

make

 

./edit

 

五、实验体会

实验比较简单,gcc命令需要加强练习。

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...