南京邮电大学嵌入式系统开发实验1:嵌入式开发工具使用实验

本文档详述了嵌入式开发中的关键步骤,包括arm-linux-gcc的安装与使用,Makefile的编写及make工具的运用,以及通过gdb进行应用程序的调试。实验涵盖了从源码编译到错误排查的全过程,旨在提升开发者在Linux环境下对嵌入式应用的开发和调试能力。
摘要由CSDN通过智能技术生成

实验1  嵌入式开发工具使用实验

.实验目的

了解嵌入式开发工具套件组成,掌握开发工具安装,熟练运用gcc各命令选项,熟练编写Makefile和使用make工具,掌握gdb各命令用于应用程序调试。

.实验内容

实验2.1  arm-linux-gcc安装

实验2.2  编译工具gcc使用

实验2.3  编写Makefile和使用make编译

实验2.4  使用gdb调试应用程序

实验2.5  使用code::blocks进行图形化编程

.预备知识

Linux使用等

.实验设备及工具(包括软件调试工具)

硬件:ARM 嵌入式开发平台、PC 机Pentium100 以上、串口线。

软件: WinXP或UBUNTU开发环境。

.实验步骤

5.1 交叉编译工具配置及编译

步骤【参看04- Tiny6410 Linux开发指南.pdf文档1.3.5节】:

第一步,解压缩交叉编译器工具,命令为: #tar xvzf arm-linux-gcc-4.5.1-v6-vfp-20101103.tgz,arm-linux-gcc可执行文件位于目录/opt/FriendlyARM/toolschain/4.5.1

第二步,修改PATH环境变量,将arm-linux-gcc可执行文件目录添加到PATH环境变量中,命令为_export PATH=$PATH:/opt/opt/FriendlyARM/toolschain/4.5.1/bin__。

第三步,执行arm-linux-gcc –v可以看到交叉编译器版本为4.5.1

5.2  arm-linux-gcc编译工具使用

【参看相关视频文件及指导书】:

要求:要求编写冒泡排序程序bubble.c及其头文件bubble.h和主程序main.c,各文件主要内容如下:

main.c中定义数组并赋值(数组元素个数和赋值内容可以自己定义),调用bubble.c中冒泡排序函数bubble(int *p,int n)进行排序,调用输出打印函数print(int *p,int n)进行输出。

bubble.c定义bubble(int *p,int n)和print(int* p ,int n)函数,分别完成冒泡排序功能和数组输出功能。

bubble.h完成bubble.c中函数的声明。

   第一步,在/opt/exp_2文件夹下按照上述要求编写源码;

   第二步 编译

(1)编译源码文件为可执行文件bubble,命令为:

_ gcc -o bubble main.c bubble.c __。

(2)假设在/opt/exp_2文件夹下新建子目录include,并将bubble.h移到该文件夹下,编译可执行文件bubble,命令为:

_ gcc -o bubble main.c bubble.c -I./include_。

(3)假如将bubble.c编译成动态库libbubble.so,具体命令为:

arm-linux-gcc  –shared  -fpic  -o libbubble.so bubble.c –I./include

        此时利用libbubble.so编译可执行文件bubble,具体命令为:

rm bubble.c

rm bubble

arm-linux-gcc –shared -fpic -o libbubble.so bubble.c –I./include ___。

mv libbubble.so /usr/lib

gcc -o bubble main.c -lbubble -I./include

        思考:如果将bubble拷贝到开发板上,执行是否成功?怎样才能成功?

(4)可执行程序bubble生成过程中,分别使用-O1/-O2/-O3进行编译,生成的可执行文件名分别为bubble1 、bubble2和bubble3,观察这几个文件的大小分别为___7412_____字节。如果用-g命令选项生成可执行文件bubble4,则字节大小为_8160__字节。

        思考:为何这几个可执行文件大小有差异

 加上-g选项以后,gcc在编译时会做以下额外的操作:

(1)创建符号表,符号表包含了程序中使用的变量名称的列表。

(2)关闭所有的优化机制,以便程序执行过程中严格按照原来的C代码进行

5.3 编写Makefile和使用make工具

        按照5.2要求,编写Makefile文件,要求能够完成源码文件编译和中间文件清除。

(1)Makefile如下

SRCS:=$(wildcard *.c)

OBJS:=$(patsubst %.c,%.o,$(SRCS))

CC=gcc

bubble:$(OBJS)

       $(CC) -o $@  $(OBJS)

%.o:%.c

       $(CC) -g -c -o $@  $< -I./include

clean:

       rm $(OBJS) bubble

(2)使用make工具编译生成可执行文件命令为_____make____;

(3)清除中间结果命令为_____make clean____。

5.4  假设按照我们提供的实验源码bubble.c和main.c生成可执行文件,执行结果为:

the sorted Array is -1061061888 1  2  3  5  10  -1061061888

9     }

发现执行结果并不符合我们的预期,使用gdb进行调试,列出具体步骤如下:

  1.  yjl@ubuntu:/opt/exp_2$ gdb bubble     对bubble进行gdb调试

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1

Copyright (C) 2016 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 "i686-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 bubble...done.

(2)  (gdb) list                         打印代码

1    #include "bubble.h"

2   

3    void main()

4    {

5           int a[5]={3,5,2,10,1};

6          

7           bubble(a,5);

8           Print(a,5);

9    }

(3)  (gdb) b main                     在main里下断点

Breakpoint 1 at 0x80485d8: file main.c, line 4.

(4)  (gdb) run                        运行

Starting program: /opt/exp_2/bubble

Breakpoint 1, main () at main.c:4

4    {

(5)  (gdb) next                       执行下一行代码

5           int a[5]={3,5,2,10,1};

(gdb) print a[0]                   打印a[0]的值,逐步调试,查看出错代码位置

$1 = -1209841056

(gdb) next

7           bubble(a,5);

(gdb) print a[0]

$2 = 3

(gdb) next

8           Print(a,5);

(gdb) print a[0]

$3 = 1

(gdb) print a[1]

$4 = 2

(gdb) print a[3]

$5 = 5

(gdb) print a[4]

$6 = 10

(gdb) next

the sorted Array is -1061061888 1  2  3  5  10  -1061061888

9    }

(6)  (gdb) quit                          退出调试

A debugging session is active.

      Inferior 1 [process 19456] will be killed.

Quit anyway? (y or n) y

(gdb) b Print                        在Print函数内下断点,显示断点位置

Breakpoint 1 at 0x804855e: file bubble.c, line 21.

(gdb) b main

Breakpoint 2 at 0x80485d8: file main.c, line 4.

(gdb) run

Starting program: /opt/exp_2/bubble

Breakpoint 2, main () at main.c:4

4    {

(gdb) next

5           int a[5]={3,5,2,10,1};

(gdb) next

7           bubble(a,5);

(gdb) next

8           Print(a,5);

(gdb) step

Breakpoint 1, Print (pArr=0xbffff068, n=5) at bubble.c:21

21         printf("the sorted Array is %d",pArr[i]);

(gdb) next

22         for(i=0;i<=n;i++)

(gdb) next

24                printf(" %d ",pArr[i]);

(gdb) next

22         for(i=0;i<=n;i++)

经过调试发现,代码出现bug的位置在bubble.c里的Print函数,将其修改为

void Print(int *pArr,int n)

{

      int i;

      printf("the sorted Array is ");

      for(i=0;i<n;i++)

      {

             printf(" %d ",pArr[i]);

      }

      printf("\n");

}

在bubble.h头文件中加入#include<stdio.h>,即可正确运行该程序。

输出结果为:the sorted Array is   1    2    3    5   10

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cookie爱吃小饼干

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值