linux c开发工具 实验三

  1. 编写一个c语言程序:输出两行文字“Linux下的c也不是太难嘛!”。在Linux下编辑、编译、运行该程序。           

    //  gcc -o指定输出文件 默认./a.out

//return 0 0 是返回码

[s2@centos8 ~]$ vi  linux.c
[s2@centos8 ~]$ cat linux.c
#include<stdio.h>

int main()
{
      printf("Linux下的c也不是太难嘛!");
              return 0;
}

[s2@centos8 ~]$ gcc linux.c -o linux//-o指定输出文件 默认./a.out
[s2@centos8 ~]$ ./linux
Linux下的c也不是太难嘛!

2.编写一个c语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器生成它的汇编程序文件。

gcc -c avg.c//生成目标文件 avg.o

gcc -S avg.c //生成汇编文件avg.s

[s2@centos8 ~]$ vi avg.c
[s2@centos8 ~]$ gcc avg.c -o avg
[s2@centos8 ~]$ ./avg
2
2
2
[s2@centos8 ~]$ cat avg.c
#include<stdio.h>


int main()
{
      int a,b;
      scanf("%d%d",&a,&b);
      int  c;
      c=(a+b)/2;

      printf("%d\n",c);
      return 0;
}

[s2@centos8 ~]$ gcc -c avg.c
[s2@centos8 ~]$ gcc -S avg.c

  1. 用gdb调试器调试上面第2题的程序,查看程序执行每一步变量的值,熟悉gdb的使用流程。

gdb  :

//进入gdb  在编译过程中加上-g

gcc  avg.c -o avg -g 

gdb avg//进入gdb

(gdb) l   //显示程序内容

(gdb) b  //设置断点 后面跟行号或者函数

(gdb)  r //运行程序

(gdb) info b//查看断点信息

(gdb)p 变量名 //查看指定变量的值

(gdb)n/s //单步运行  区别在于n不进入函数,s进入函数

(gdb) set 变量=设定值 //变量赋值

(gdb)c //继续运行程序

(gdb)q//退出gdb环境

[s2@centos8 ~]$ gcc  avg.c -o avg -g
[s2@centos8 ~]$ gdb avg
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-11.el8
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from avg...done.
(gdb) r
Starting program: /home/s2021013043/avg
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-101.el8.x86_64
1
2
1.000000
[Inferior 1 (process 100690) exited normally]
(gdb) b 1
Breakpoint 1 at 0x4005fe: file avg.c, line 7.
(gdb) r
Starting program: /home/s2021013043/avg

Breakpoint 1, main () at avg.c:7
7             scanf("%d%d",&a,&b);
(gdb) n
1
1
9             c=(a+b)/2;
(gdb) c
Continuing.
1.000000
[Inferior 1 (process 100835) exited normally]
(gdb) r
Starting program: /home/s2021013043/avg

Breakpoint 1, main () at avg.c:7
7             scanf("%d%d",&a,&b);
(gdb) n
1
1
9             c=(a+b)/2;
(gdb) p c
$1 = 0
(gdb) c
Continuing.
1.000000
[Inferior 1 (process 100876) exited normally]
(gdb)
(gdb) l
4       int main()
5       {
6             int a,b;
7             scanf("%d%d",&a,&b);
8             float  c;
9             c=(a+b)/2;
10
11            printf("%f\n",c);
12            return 0;
13      }
  1. 建立一个简单程序,练习函数库创建方法。函数库包含两个函数,并在一个示例程序(prom.c)中调用,这两个函数分别是fast.c(快速排序)、insert.c(插入排序),实现对一维数组的升序排序。按下面步骤生成函数库。

(1),分别编写prom.c、fast.c、insert.c;

 

[s2@centos8 ~]$ cat fast.c
#include<stdio.h>
int  fast(int a[],int m,int n)
{
    if(m>=n)
            return 0;
    int left=m;
    int right=n;
     int q=a[m];
     while(left<right)
     {
     while(left<right&&a[right]>q)
             right--;
     while(left<right&&a[left]<q)
             left++;

     int t=a[left];
     a[left]=a[right];
     a[right]=t;
     }
    int t=a[left];
    a[left]=q;
    q=t;
     fast(a,m,left-1);
     fast(a,right+1,n);

}

[s2@centos8 ~]$ cat insert.c
#include<stdio.h>

int insert(int a[],int n)
{
        int i,j;
        for(i=2;i<n;i++)
        {
            j=i-1;
            a[0]=a[i];
            while(a[i]<a[j])
            {
              a[j+1]=a[j];
              j--;
            }
            a[j+1]=a[0];
        }
        return 0;
}

[s2@centos8 ~]$ cat prom.c
int main()
{
        int a[10]={9,8,7,6,5,4,3,2,1,0 };
        fast(a,0,10);
        for (int i = 1; i < 11; i++)
        {       printf("%2d ",a[i]);
        }
        insert(a,10);
        for (int i = 1; i < 11; i++)
        {       printf("%2d ",a[i]);
        }
        return 0;}

2)编译这两个程序,产生目标文件fast.o、insert.o

gcc -c fast.c insert.c

3)创建库文件libfoo.a

ar crv libsort.a fast.o insert.o

4)不使用库编译prom.c

gcc -c prom.c

gcc prom.o fast.o  insert.o -o prom1

5)使用库文件libsort.a编译prom.c

gcc -o prom2 prom.o libsort.a

6)使用库文件libsort.a编译prom.c,用l选项访问函数库,用L选项指示库文件所在目录

gcc -o prom3 prom.o -L . -lsort

7)试着编写一个Makefile规则文件管理该工程文件,并使用make编译为prom4;

//目标文件: 依赖文件

Tab 产生目标文件的命令

prom4: prom.o fast.o insert.o   
gcc prom.o fast.o insert.o -o prom4
prom.o: prom.c
  gcc -c prom.c
fast.o:fast.c 
  gcc -c fast.c
insert.o:insert.c   
    gcc -c insert.c

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值