如何使用Buildroot交叉编译MIPS32架构的GDB Server?
我来一步步帮你解决这两个核心问题:先是configure没识别交叉编译,再是-lthread_db缺失导致编译失败,同时保住多线程调试能力。
1. 先让configure正确识别交叉编译
你的configure命令没认出交叉编译,大概率是工具链检测逻辑没拿到足够的目标系统信息,试试这么调整:
- 先把Buildroot工具链的路径加到
PATH里,再设置好交叉前缀,让configure能自动推导所有工具,比手动指定AR/LD更靠谱:export CROSS=mips-buildroot-linux-uclibc- export PATH=/root/Desktop/buildroot/buildroot-2018.02.2/output/host/bin:$PATH - 然后简化configure命令,关键要加上
--with-sysroot,指定Buildroot的staging目录作为目标系统的根文件系统,这样configure就不会误搜主机的库和头文件了:
跑完之后看输出里的./configure --host=mips-buildroot-linux-uclibc \ --with-sysroot=/root/Desktop/buildroot/buildroot-2018.02.2/output/staging \ LIBS="-L/root/Desktop/buildroot/buildroot-2018.02.2/output/staging/lib"checking whether we are cross compiling... yes,出现这个就说明交叉编译识别成功了。
2. 解决-lthread_db找不到的问题
libthread_db是多线程调试的核心依赖库,uClibc的这个库藏在Buildroot的staging目录里,得先确认它存在,再让编译器找到它:
先检查Buildroot有没有编译这个库:去
/root/Desktop/buildroot/buildroot-2018.02.2/output/staging/lib下看看有没有libthread_db.so或者libthread_db.a。如果没有,就得去Buildroot配置里开启:
进入Buildroot目录,执行make menuconfig,然后找:Target packages -> Debugging, profiling and benchmark -> gdb
先别选gdbserver(毕竟你要手动编译),然后确保uClibc开了线程支持:
Toolchain -> C library -> uClibc -> Enable thread support
开了线程支持之后,libthread_db一般会自动编译。保存配置后执行make,重新生成工具链和staging目录。如果已经有这个库了,编译时把staging的lib目录加到LDFLAGS里就行:
make LDFLAGS="-static -L/root/Desktop/buildroot/buildroot-2018.02.2/output/staging/lib"注意:要是uClibc的
libthread_db只有动态版本,静态编译可能搞不定——要么放弃静态编译,要么确保目标设备上有对应的动态库。
3. 偷懒又靠谱的方法:直接让Buildroot帮你编译GDB Server
其实Buildroot本身就支持编译GDB Server,完全不用手动折腾交叉编译,兼容性还好:
- 进入Buildroot目录,执行
make menuconfig - 找到:
Target packages -> Debugging, profiling and benchmark -> gdb
勾选gdbserver,保存配置 - 执行
make,等编译完成后,GDB Server就在output/target/usr/bin/gdbserver里了,直接拿过去用就行,所有依赖(包括libthread_db)都会自动处理好。
内容的提问来源于stack exchange,提问作者CS Student

