31

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

#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

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.

3
  • 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. Commented Dec 4, 2017 at 18:31
  • file ./hello gives what? Commented Dec 4, 2017 at 18:32
  • 11
    This GDB was configured as "i686-linux-gnu". you are probably trying to debug 64-bit executable with with 32-bit configured gdb. Try to build 32-bit executable: g++ -m32 -g TestProject.cpp -o hello or install 64-bit configured gdb. Commented Dec 4, 2017 at 20:22

3 Answers 3

20

It is almost certain that ks1322's comment is correct one:

  1. You've installed a 64-bit GCC, so your ./hello is a 64-bit binary (use file ./hello to confirm).
  2. You've installed a 32-bit only GDB, so it doesn't know how to debug x86_64 binaries.

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

Sign up to request clarification or add additional context in comments.

3 Comments

Note to self: don't compile it on Mac and then try to run it on Linux just because Github allows you to share code between the two!
the second bullet point worked for me
how to install 64 bit gdb?
7

I had the same issue on mac os. there is a bug in gdb: https://sourceware.org/bugzilla/show_bug.cgi?id=23746 their 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.

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 (

Comments

2

There is the possibility that you are using autotools (configure etc.) to build your executable. But until you install it, it is run via a wrapper script.

In this case you need to run something like this:

libtool --mode=execute gdb executable ...

If this is the case then you can easily check the contents of the executable to see if it is a wrapper script.

See How to debug a program wrapped in a libtool script?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.