即使使用-g选项,GCC也不会生成行号信息

Cal*_*ius 7 c gcc gdb gcc4.8

我从源代码构建并安装了GCC 4.8.1:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.8.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --disable-multilib
Thread model: posix
gcc version 4.8.1 (GCC) 
Run Code Online (Sandbox Code Playgroud)

我写了一个简单无用的程序:

$ cat hw.c
#include <stdio.h>

void foo()
{
    int a;
    scanf("%d", &a); /* So I can press ctrl+c here. */
    printf("Hello world!\n");
}

int main()
{
    foo();   
}
Run Code Online (Sandbox Code Playgroud)

现在我编译这个:

$ gcc -g -O0 hw.c -o hw
Run Code Online (Sandbox Code Playgroud)

然后开始用GDB调试它:

$ gdb hw
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 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".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/calmarius/workdir/crucible/hw/hw...done.
(gdb) 
Run Code Online (Sandbox Code Playgroud)

运行它并立即按Ctrl + C:

(gdb) run
Starting program: /home/dcsirmaz/workdir/crucible/hw/hw 
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7b018b0 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:82
82  ../sysdeps/unix/syscall-template.S: Nincs ilyen fájl vagy könyvtár.
Run Code Online (Sandbox Code Playgroud)

我在回溯中有函数名但在我的代码中没有行号:

(gdb) bt
#0  0x00007ffff7b018b0 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:82
#1  0x00007ffff7a95ff8 in _IO_new_file_underflow (fp=0x7ffff7dd4340) at fileops.c:619
#2  0x00007ffff7a9703e in _IO_default_uflow (fp=0x7ffff7dd4340) at genops.c:440
#3  0x00007ffff7a74fb6 in _IO_vfscanf_internal (s=<optimized out>, format=<optimized out>, argptr=0x7fffffffe018, errp=0x0) at vfscanf.c:620
#4  0x00007ffff7a790bd in __isoc99_scanf (format=<optimized out>) at isoc99_scanf.c:37
#5  0x000000000040054e in foo ()
#6  0x0000000000400568 in main ()
Run Code Online (Sandbox Code Playgroud)

出了什么问题?也许是配置的东西?

Chr*_*odd 16

你的gdb太旧了 - 你需要一个更新的gdb(我使用7.6)来理解gcc 4.8.1生成的调试信息

  • 更准确地说,GCC-4.8默认使用dwarf4(http://gcc.gnu.org/gcc-4.8/changes.html),但你的GDB太老了,无法理解.使用`-gdwarf-2`构建,您将获得您的行号.或者从源代码构建中将GDB更新为更新版本. (3认同)

小智 9

通常GCC使用dwarf作为其主要调试文件格式,在使用标志构建gcc时需要启用dwarf支持--with-dwarf2.

在构建编译对象时,您可以使用-ggdb而不是-g更具体的解决方案,但仅限于此gdb.

  • 实际上,dwarf 是问题所在——他使用的是 Ubuntu 的旧版本 gdb,它无法理解最新版本的 gcc 生成的调试信息。 (2认同)