Linux编程环境
创始人
2024-04-19 21:14:40

一、实验目的

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命令需要加强练习。

相关内容

热门资讯

北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
阿西吧是什么意思 阿西吧相当于... 即使你没有受到过任何外语培训,你也懂四国语言。汉语:你好英语:Shit韩语:阿西吧(아,씨발! )日...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...