GDB错误不以可执行格式:未识别的文件格式

gdb error not in executable format: File format not recognized

本文关键字:格式 识别 文件 GDB 可执行 错误      更新时间:2023-10-16

我正在尝试在Ubuntu 16.04上调试一个简单的" Hello World" C 程序,但GDB无法识别可执行文件格式。但是,我能够在命令行上成功运行可执行文件。这是代码

#include <iostream>
using namespace std;
int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

我使用命令

编译程序文件testproject.cpp
g++ -g TestProject.cpp -o hello

然后进行调试,我给命令

gdb ./hello

我收到以下错误消息

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
"/home/<home>/./hello": not in executable format: File format not recognized

ubuntu机器似乎损坏了。因为我能够在另一台Ubuntu 16.04虚拟机上调试同一程序。

几乎确定KS1322的评论是正确的:

  1. 您已经安装了64位GCC,因此您的./hello是64位二进制(使用file ./hello确认)。
  2. 您已经安装了32位GDB,因此它不知道如何调试x86_64二进制文件。

修复程序很简单:安装64位GDB(能够同时调试32和64位二进制文件),以32位模式构建hello(使用g++ -m32 ...)。

我在Mac OS上也有相同的问题。GDB中有一个错误:https://sourceware.org/bugzilla/show_bug.cgi?id=23746他们的GIT存储库已经有了修复。不幸的是,Homebrew中的垃圾箱还没有。因此,我不得不git clone git://sourceware.org/git/binutils-gdb.git,对其进行了编译并安装,如readme文件中所述。我确实相信这将在Ubuntu上解决您的问题。

P.S。它在我的机器上工作,但我必须以root的蚀态运行日食:sudo/.../macos/eclipse。否则,我将启动:配置GDB中止配置GDB。因为我不知道如何修复它(