我从源代码构建并安装了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 …Run Code Online (Sandbox Code Playgroud) 我对gdb有些麻烦.这是我在名为main.cpp的单个文件中的代码
#include <iostream>
void myfunc();
int main(){
char msg[] = "Hello World!";
myfunc();
std::cout << msg << std::endl;
return 0;
}
void myfunc(){
int boo = 16;
}
Run Code Online (Sandbox Code Playgroud)
我用这个命令来编译这段代码:
g++ -g -Wall main.cpp -o foo
Run Code Online (Sandbox Code Playgroud)
接下来,我使用了gdb:
$ gdb foo
(gdb) start
Temporary breakpoint 1 at 0x80487c3
Starting program: /home/laptop/workspace/foo
Temporary breakpoint 1, 0x080487c3 in main ()
(gdb) s
Single stepping until exit from function main,
which has no line number information.
Hello World!
__libc_start_main (main=0x80487c0 <main>, argc=1, ubp_av=0xbffff3a4, init=0x80488b0 <__libc_csu_init>, …Run Code Online (Sandbox Code Playgroud)