我刚刚转移到 ubuntu 并新使用 gdb 和 g++ 。如果我的问题很愚蠢,请原谅我。
这是来自 Richard Stevens 高级 Linux 编程。三个文件分别创建在文件夹名称互惠
main.c
:
#include <stdio.h>
#include "reciprocal.hpp"
int main (int argc, char **argv)
{
int i;
i = atoi (argv[1]);
printf ("The reciprocal of %d is %g\n", i, reciprocal (i));
return 0;
}
互惠.cpp
:
#include <cassert>
#include "reciprocal.hpp"
double reciprocal (int i) {
// I should be non-zero.
assert (i != 0);
return 1.0/i;
}
倒数.hpp
:
#ifdef __cplusplus
extern "C" {
#endif
extern double reciprocal (int i);
#ifdef __cplusplus
}
#endif
编译后,我运行命令 (gdb) reciprocal
和 (gdb) run
。我在期待书中的内容
Starting program: reciprocal
Program received signal SIGSEGV, Segmentation fault.
__strtol_internal (nptr=0x0, endptr=0x0, base=10, group=0)
at strtol.c:287
287 strtol.c: No such file or directory.
(gdb)
但是我得到了:
Starting program: /home/trafalgar/Desktop/reciprocal/reciprocal
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Program received signal SIGSEGV , Segmentation fault.
0x00007ffff7a56ad4 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
可能会发生什么不同。这是版本问题还是其他问题?
这是生成文件
reciprocal: main.o reciprocal.o
g++ $(CFLAGS) -o reciprocal main.o reciprocal.o
main.o: main.c reciprocal.hpp
gcc $(CFLAGS) -c main.c
reciprocal.o: reciprocal.cpp reciprocal.hpp
g++ $(CFLAGS) -c reciprocal.cpp
clean:
rm -f *.o reciprocal
最佳答案
你是如何编译程序的?
使用 g++ -g 程序名.c
还有,当你这样做的时候
gdb互惠
注意是否有这样的消息
从...加载符号
或者
找不到符号
如果你得到类似于第二个代码语句的输出,那么问题是你没有使用 -g 符号。
关于linux - gdb 没有给出错误的完整描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22170186/