使用常规的gcc编译或者使用带调式信息-g的编译都有可能会在使用gdb工具的时候出现No debugging symbols found in a.out错误。

1. 常规编译

使用常规编译生成的可执行文件正常一定会出现No debugging symbols found in a.out错误。

imaginemiracle@ubuntu:test$ ls
test.c
imaginemiracle@ubuntu:test$ gcc test.c 

imaginemiracle@ubuntu:test$ gdb a.out 
Reading symbols from a.out...
(No debugging symbols found in a.out)
(gdb) 

2.带调试编译

即使使用带调试信息-g编译时,有时候也会出现这种情况。

imaginemiracle@ubuntu:test$ gcc -g test.c 
imaginemiracle@ubuntu:test$ ls
a.out  test.c

imaginemiracle@ubuntu:test$ gdb a.out 
Reading symbols from a.out...
(No debugging symbols found in a.out)
(gdb) 

3. 解决办法

我们先使用-c -g生成带调试信息的只进行过预处理、编译、汇编,但没有被链接的目标文件,再利用目标文件链接、重定位后生成可执行文件,使用这个可执行文件便不会出现问题。

imaginemiracle@ubuntu:test$ rm a.out 
imaginemiracle@ubuntu:test$ gcc -c -g test.c 
imaginemiracle@ubuntu:test$ ls
test.c  test.o
imaginemiracle@ubuntu:test$ gcc test.o
maginemiracle@ubuntu:test$ gdb a.out 
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 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-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 a.out...
(gdb) l
1	int main(int argc, char **argv)
2	{
3	    int a = 10;
4	    int b = 20;
5	
6	    int result = 0;
7	
8	    result = a + b / a;
9	
10	    return 0;
(gdb) 
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐