I am new to Ubuntu and am using the 12.04 version on my laptop. I am trying to execute a C++ program which has Petsc libraries installed in it. I get an error when trying to run the program and so have installed GDB in Ubuntu.

The name of the program is ex1.c and is located in a folder called 'petsc-3.1-p8' on my computer. How do I run gdb in the terminal to see what is the error while I run this program?

Thanks in advance..


Based on the file extensions (.c), I assume that ex1.c is a C source file. You must first compile the source code before you can debug the application with GDB. Assuming you have GCC installed (available from the gcc package Install gcc), you can do this with:

gcc ex1.c -g -o ex1

Once compiled, you can debug the application by running:

gdb ex1

This will load the executable and dump you at the GDB prompt. You can then start the application by running the following command at the prompt:


If the program crashes, you can inspect the calling stack by running:

| improve this answer | |
  • Should the gcc line also include -g? – chaskes Jun 27 '13 at 19:29
  • @chaskes: Ah, good point. I'll add that to my answer. – Nathan Osman Jun 27 '13 at 19:31
  • Nathan, thanks for the reply. I am able to compile the code using the command 'make ex1'.I am getting an error while executing the code using 'mpiexec -n 1 -m 200 -n 80 -q 32 -ntimes 10'. So, do I have to run the GDB command after executing the code or after compiling the code? Please bear with me, I am a newbie and have a lots of doubts. Thanks in advance. – user170775 Jun 28 '13 at 14:46
  • @user170775: The code must be compiled before running GDB. GDB itself will execute the code when you enter the "run" command. – Nathan Osman Jun 28 '13 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.