gdb是功能非常强大的常用调试工具,可以直接下断点进行单步调试,是差错排错的利器。交叉编译工具链编译出来的嵌入式程序就是放在嵌入式设备上运行的程序。
本文章仅仅就如何正确的编译出所需平台的gdb工具进行说明,对于工具的使用方法等不在此文章范围,文章中的XXX按照自己实际路径进行修正。
1、首先需要下载gdb的源码
源码下载可以从gdb的官网进行下载,下载地址如下:
https://sourceware.org/pub/gdb/releases/?C=M;O=D
从下载列表中选择所需要的版本即可。本文中使用11.2版本。
使用 tar -xvf gdb-11.2.tar.gz进行解压
编译gdb工具时需要连接libgmp库,否则在编译的时候会提示没有gmp库而报错并终止编译动作,提示如下:
checking size of unsigned long... 4
checking size of unsigned __int128... 0
checking for library containing dlopen... none required
checking whether to use expat... auto
checking for libexpat... yes
checking how to link with libexpat... -lexpat
checking for XML_StopParser... yes
checking for libgmp... no
configure: error: GMP is missing or unusable
make[1]: *** [Makefile:10420:configure-gdb] 错误 1
make[1]: 离开目录“/home/XXX/gdb-11.2”
make: *** [Makefile:903:all] 错误 2
故我们第一步需要先编译出libgmp库做好准备
使用命令wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2从ftp服务器上获取libgmp库的源码并放到需要的路径
tar -xvf gmp-6.1.0.tar.bz2进行解压,解压后进入到gmp-6.1.0目录内依次执行如下命令
./configure --host=arm-sigmastar-linux-uclibcgnueabihf --prefix=/home/XXX/opt/gmp
make clean
make -j18
make install
执行完后编译gdb需要的头文件和库都在安装目录/home/XXX/opt/gmp下。
进入到上文要编译的gdb-11.2目录,然后在此目录中依次执行
export LDFLAGS="-L/home/xxx/opt/gmp/lib"
export CPPFLAGS="-I/home/xxx/opt/gmp/include"
./configure --prefix=/home/xxx/opt/gdb --host=arm-sigmastar-linux-uclibcgnueabihf --target=arm-sigmastar-linux-uclibcgnueabihf
make -j18
make install
两个export是连接编译gdb工具时需要连接的gmp库文件和头文件,即上一步的安装目录(可自行调换),使用configure中--with-gmp并不好使,还是会提示没有gmp库文件,使用外部引用的方式可以。
执行后会在gdb的安装路径/home/xxx/opt/gdb/bin中找到需要运行的gdb文件,这个时候gdb文件可能会比较大,因为没有strip,运行开发平台工具链的strip进行处理,处理后gdb文件大小在4M多,大小可接受。
把编译出来的gdb文件和libgmp.so.10拷贝到开发板中运行,发现提示缺少libncurses.so.6库
can't load library 'libncurses.so.6'
那么再进行libncurses.so.6库的编译,这个库本身是一个依赖文件,编译完这个库直接放到开发版即可,不需要再重新编译gdb工具
从https://invisible-island.net/archives/ncurses/连接中下载ncurses库的源码,本作者选择的6.2版本
相同的处理
tar -xvf ncurses-6.2.tar.gz进行源码解压
进入到源码文件夹cd ncurses-6.2
./configure --prefix=/home/XXX/opt/ncurses --host=arm-sigmastar-linux-uclibcgnueabihf
进行配置编译工具链和安装路径
make clean
make -j18
make install
在install时最后可以能提示strip进程异常终止,只要安装路径有需要的头文件和库即可,不用理它。
把安装路径中的libncurses.so.6导入到开发版中运行,出现了新的错误提示
can't load library 'libexpat.so.1'
又缺少一个库,这个库和刚才的libncurses.so.6库一样的处理办法
从https://github.com/libexpat/libexpat/releases中下载需要的代码源码,本作者选择2.2.10版本。
下载完tar -xvf libexpat-R_2_2_10.tar.gz解压
解压完进入libexpat-R_2_2_10/expat目录下,在此目录下没有configure文件
./buildconf.sh执行自动配置脚本后出现configure文件
相同的步骤进行编译
./configure --prefix=/home/XXX/opt/expat --host=arm-sigmastar-linux-uclibcgnueabihf
make clean
make -j18
make install
在安装路径下找到libexpat.so.1并拷贝到开发版中。
这个时候发现gdb工具可以正常使用了
./gdb
GNU gdb (GDB) 11.2
Copyright (C) 2022 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 "arm-sigmastar-linux-uclibcgnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://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".