我会质疑您未显示的先前设置步骤。如果您的朋友能够在那里设置断点而您没有,则可能是您做错了什么。(假设每个人都使用相同版本的所有工具)
使用适用于 Ubuntu 和 gcc 4.6.3 的 gdb 版本 7.4-2012.04 我可以看到并在相关行设置断点:
> gcc -Wall -g file.c <-- compile with -g for debug symbols
> gdb a.out <-- run against the executable
This GDB was configured as "x86_64-linux-gnu". <-- make sure it was configured for
For bug reporting instructions, please see: your architecture
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/mike/C/a.out...done. <-- and that your file loads symbols
(gdb) line 22, 39
22 int swap_n_add(int *xp, int *yp) <-- mine are different because I have a bunch
23 { of #include's in my test file
24 int x = *xp;
25 int y = *yp;
26
27 *xp = y;
28 *yp = x;
29 return x + y;
30 }
31
32 int main() {
33 int a1 = 534;
34 int a2 = 1057;
35 int sum = swap_n_add(&a1, &a2);
36 int diff = a1 - a2;
37
38 return sum * diff;
39 }
(gdb) b 39
Breakpoint 1 at 0x400530: file file.c, line 39.
请注意,如果我在文件之外选择一行,比如.. 75,它会给你一条关于“文件中没有第 x 行”的消息:
(gdb) b 75
No line 75 in the current file.
Make breakpoint pending on future shared library load? (y or [n])
如果您看到这一点,值得仔细检查您的行号,您可能算错了。
如果您想查看 return ( sum * diff
) 的值,您始终可以在返回之前将其设置为局部变量并在返回时中断。