1. 嵌入式Linux开发工具——调试器gdb
-
gdb也是GNU计划(a\启动运行程序 b\设置断点 c\查看变量值)
-
使用gdb之前:需要-g编译选项
-
(gdb)run运行,(gdb)list回车显示每次断点的代码
-
学习gdb命令:gdb 可执行文件 --tui
-
运行命令:run(r); continue(c)
-
设置断点:break 行号 或 break 函数名 或 break 行号 条件
-
查看断点信息:info break
-
删除断点:delete 断点号
-
单步执行:next(n); step(s)
n单步执行,从15行处的断点跳到17行处(即下一步)
s和n的作用时一样的,从17行跳到15行(因为断点在循环里)
n和s的区别:n不进入子函数(跳过子函数);s进入子函数 -
查看变量值:print 变量名
2. 嵌入式Linux开发工具——工程管理器make -
什么是工程/项目?
项目:多个源文件、资源文件构成的项目代码 -
如何编译多个源文件的代码?
1000个源文件,并且不在同一目录——工程管路器make -
工程管路器make的作用:自动编译
-
makefile文件的作用:存放编译项目的命令(如何编译这个项目的所有操作)
-
makefile是一个脚本文件:批处理
-
makefile语法:三要素——目标、依赖、命令(执行命令,根据依赖的文件生成对应的目标)
语法格式:
目标:依赖
[tab] 命令
如下图:
1 hello:hello.c
2 gcc hello.c -o hello
执行顺序:make 目标名称(从目标名称下开始执行);如果目标名称不存在就从下一行开始执行
1 hello:hello.o
2 gcc hello.o -o hello
3 hello.o:hello.c
4 gcc -c hello.c -o hello.o
编辑好makefile后,输入命令make,结果如下:
如果上面的代码调换顺序,就只能执行一条命令。
1 hello.o:hello.c
2 gcc -c hello.c -o hello.o
3 hello:hello.o
4 gcc hello.o -o hello
执行make命令后,结果如下:
注意:make执行的时候,首先看第一行,如果第一行的目标文件能生成,那么就不会执行下面的命令。所以编辑makefile时,把后生成的目标文件写在前面。
设置假目标文件(不存在当前目录的文件),用.PHONY:clean。clean中的命令是用来删除hello和hello.o文件,以使得每次make时,直接用make clean来删除,而不用输入命令rm -f hello hello.o,这样能提高效率。实践操作如下图:
- 如何隐藏命令:在命令前加@
1 hello:hello.o
2 @gcc hello.c -o hello
3 echo "make done!"
4 hello.o:hello.c
5 @gcc -c hello.c -o hello.o
6 .PHONY:clean
7 clean:
@rm -rf hello hello.o
在echo "make done!"前加@,将echo "make done!"命令隐藏后的结果截图如下:
关于makefile中的格式写法
hello:hello.o
hello +=hello.o
hello :=hello.o
题外话:关于加减乘除中make时产生的警告,如何修改
产生的警告如下图:
解决方法:
在当前目录下新建一个main1.h(注意不要起名为main.h),相当于新建了一个库,作用是对所有的函数进行函数申明,代码如下:
1 #ifndef MAIN_H //MAIN的起名是根据main.c,要求它们名字一样
2 #define MAIN_H
3
4 #include <stdio.h>
5
6 int add(int a,int b);
7 int sub(int a,int b);
8 int mul(int a,int b);
9 int div(int a,int b);
10
11 #endif
然后在add.c,sub.c,mul.c,div.c,main.c中头文件中增加#include “main1.h”。
main.c代码如下:
#include <stdio.h>
#include "main1.h"
int main()
{
printf("add = %d\n", add(9, 5));
printf("sub = %d\n", sub(9, 5));
printf("mul = %d\n", mul(9, 5));
printf("div = %d\n", div(9, 5));
return 0;
}
add.c代码如下:
#include "main1.h"
int add(int a,int b)
{
return a + b;
}
sub.c代码如下:
#include "main1.h"
int sub(int a, int b)
{
return a - b;
}
mul.c代码如下:
#include "main1.h"
int mul(int a, int b)
{
return a * b;
}
div.c代码如下:
#include "main1.h"
int div(int a, int b)
{
return a / b;
}
这样就能正常运行,不会出现警告,如下图: