C++ gdb 错误不是可执行格式:无法识别文件格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47639685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:42:43  来源:igfitidea点击:

gdb error not in executable format: File format not recognized

c++gdbubuntu-16.04

提问by Ashutosh Pandey

I am trying to debug a simple "hello world" C++ program on Ubuntu 16.04 but gdb is not able to recognize the executable file format. However, I am able to successfully run the executable on the command line. Here is the code

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

#include <iostream>
using namespace std;

int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

I compile the program file TestProject.cpp using the command

我使用命令编译程序文件 TestProject.cpp

g++ -g TestProject.cpp -o hello

Then to debug, I give the command

然后调试,我给出命令

gdb ./hello

I get the following error message

我收到以下错误消息

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

Something seems to be corrupt with the Ubuntu machine. Because I am able to debug the same program on another Ubuntu 16.04 virtual machine.

Ubuntu 机器似乎有问题。因为我能够在另一个 Ubuntu 16.04 虚拟机上调试相同的程序。

回答by Employed Russian

It is almostcertain that ks1322's comment is correct one:

这是几乎可以肯定,ks1322的评论是正确的:

  1. You've installed a 64-bit GCC, so your ./hellois a 64-bit binary (use file ./helloto confirm).
  2. You've installed a 32-bit only GDB, so it doesn't know how to debug x86_64binaries.
  1. 您已经安装了 64 位 GCC,因此您./hello是 64 位二进制文​​件(用于file ./hello确认)。
  2. 您只安装了 32 位 GDB,因此它不知道如何调试x86_64二进制文件。

The fix is simple: install 64-bit GDB (which is capable of debugging both 32 and 64-bit binaries), orbuild helloin 32-bit mode (with g++ -m32 ...).

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

回答by Boris

I had the same issue on mac os. there is a bug in gdb: https://sourceware.org/bugzilla/show_bug.cgi?id=23746their git repository has already had the fix. Unfortunately, the bins in homebrew have not had it yet. So, I had to git clone git://sourceware.org/git/binutils-gdb.git, compiled it and installed as it is described in the readme file. I do believe this will fix yours on ubuntu.

我在 mac os 上遇到了同样的问题。gdb 中有一个错误:https: //sourceware.org/bugzilla/show_bug.cgi?id =23746他们的 git 存储库已经修复了。不幸的是,自制软件中的垃圾箱还没有。所以,我不得不 git clone git://sourceware.org/git/binutils-gdb.git,编译它并按照自述文件中的描述进行安装。我相信这会在 ubuntu 上解决您的问题。

P.S. it works on my machine but I have to run eclipse as root: sudo /.../MacOS/eclipse. Otherwise, I have Launching : Configuring GDB Aborting configuring GDB. Cause I do not know how to fix it (

PS 它可以在我的机器上运行,但我必须以 root 身份运行 eclipse:sudo /.../MacOS/eclipse。否则,我将启动:配置 GDB 中止配置 GDB。因为我不知道如何解决它(