linux - gdb 没有给出错误的完整描述

标签 linux gdb g++

我刚刚转移到 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/

相关文章:

linux - Bash 脚本如果没有像我预期的那样处理

c++ - 转储内存以查找 C++ 应用程序中的内存泄漏

xcode - .gdbinit 配置文件与 Xcode 调试冲突

c++ - 纯虚函数和三杠杆继承

c++ - 对 cv::Mat::Mat() 的 undefined reference

linux - bash 重命名空间 - 也做子文件和文件夹

linux - OMAP 4430 Linux 与 DSP 的结合使用

linux - 当脚本作为crontab作业运行时,某些程序不是由bash脚本启动的,但是可以通过手动运行bash脚本来启动所有程序

linux - 在堆栈上查找数组/缓冲区的绝对大小

c++ - 使用 G++ 动态编译 C++ 代码片段