1

I am new to using gdb so I wanted to start by using a simple program that prints "Hello"

#include<stdio.h>
main(){
printf("Hello!\n");
}

saving as hello.c and then typing gdb. Once opened, based from tutorials, I type "file hello.c" in order to load the program into the debugger, but I get this message:

This GDB was configured as "x86_64-linux-gnu".
"hello.c": not in executable format: File format not recognized

So I typed in the "gcc -Wall -g hello.c -o hello" and got this message:

hello.c:3:1: warning: return type defaults to âintâ [-Wreturn-type] hello.c: 
In function âmainâ: hello.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]

so then I edited main with int main and added return 0 in the code. I did it again, no errors so I typed gdb ./hello and it worked...should I always have my mains as int main with the return 0 included?

3
  • 3
    You debug compiled executables, not source code. Commented Sep 1, 2014 at 19:29
  • Your program is incorrect. Declare main as int main() or preferably int main (int argc, char**argv) Commented Sep 1, 2014 at 19:50
  • And don't forget the return 0; before the closing brace } Commented Sep 1, 2014 at 19:54

1 Answer 1

4

Install the packaged (for your system) gdb debugger and gcc compiler. On Debian and related (e.g. Ubuntu) distributions, run as root (e.g. with sudo):

 apt-get install gcc gdb build-essential

On Linux, you need (or want) to compile your hello.c with all warnings and debug info, i.e. using

 gcc -Wall -g hello.c -o hello

At that point you could get some error or warning messages from gcc; try to correct your source file hello.c and repeat the compilation again.

You should decide to use the C99 standard, then add -std=c99 after gcc and before -Wall

Check with

 ls -ls hello.c hello

that your hello executable has been generated (and is younger than the source hello.c file).

Then you start the debugger (by giving the debuggable executable) as

 gdb ./hello

You then get a (gdb) prompt. Try to put a breakpoint in main using the

 break main

GDB command. Then, run your program with run, it should reach the breakpoint at main; at that point, type help to get some help about GDB commands.

Most GDB commands can be abbreviated with their first few letters. So you could type b instead of break.

You definitely should learn about the watch gdb command, it is extremely useful in practice.


About main you should define it as int main (int argc, char**argv) and you should end it with return 0; (or some other small positive integer, for runtime error cases). Actually, you should add #include <stdlib.h> as an included header (like you do for <stdio.h>) and use return EXIT_SUCCESS;. Read also about exit(3); in general read the documentation of every function that you are using (e.g. printf(3)), and accept that you might not understand first all the documentation. You may want to install the manpages-dev package and learn about the man command.

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

4 Comments

I typed in the "gcc -Wall -g hello.c -o hello" and got this message: hello.c:3:1: warning: return type defaults to âintâ [-Wreturn-type] hello.c: In function âmainâ: hello.c:6:1: warning: control reaches end of non-void function [-Wreturn-type] so then I edited main with int main and added return 0 in the code. I did it again, no errors so I typed gdb ./hello and it worked...
Please edit your question to improve it by telling the messages (in your question, not in comments)
So the compiler warns you as it should. Your hello.c program is wrong. But use ls -ls hello to check that the hello executable has been recently generated.
Int argc, char** argv isn't necessary for this really, as with a simple hello world console input isn't exactly required.

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.