How do I debug a C program in Mint?

Forum rules
Before you post please read how to get help
Post Reply
0x2620
Level 2
Level 2
Posts: 79
Joined: Mon Jan 09, 2017 3:17 pm

How do I debug a C program in Mint?

Post by 0x2620 » Thu Feb 22, 2018 1:42 pm

I'm a Java programmer by training, trying to teach myself C. I currently use medit and compile programs manually. What are my options for debugging a C program and inspecting the state of variables at various times?

User avatar
Flemur
Level 15
Level 15
Posts: 5671
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: How do I debug a C program in Mint?

Post by Flemur » Thu Feb 22, 2018 1:46 pm

Code: Select all

$ cc -g program.c
$ gdb a.out
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less

dong
Level 1
Level 1
Posts: 7
Joined: Mon Apr 16, 2018 10:31 pm

Re: How do I debug a C program in Mint?

Post by dong » Tue Apr 17, 2018 7:49 am

debug is OK.
"A computer is like air conditioning. It becomes useless when you open Windows." - Linus Torvalds

User avatar
xenopeek
Level 24
Level 24
Posts: 22333
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: How do I debug a C program in Mint?

Post by xenopeek » Wed Apr 18, 2018 4:46 am

Indeed, gdb is the debugger you'll likely want to use. IDEs like GNOME Builder (for writing Gtk programs, like for Cinnamon, MATE and Xfce) or Qt Creator (for writing Qt programs, like for KDE) come with builtin gdb integration. You may find modules to add gdb integration available for other IDEs, like for Eclipse.

You can also run gdb standalone from the terminal as noted above. Going through its documentation https://www.gnu.org/software/gdb/documentation/ or getting a good book on it will undoubtedly help you make the most of it.

A friendly option is to use gdbgui, which you start from the terminal just like gdb, but it opens a page in your web browser where you get a GUI to interact with gdb. More info here https://github.com/cs01/gdbgui/

If you're writing very big and complex programs, Valgrind may also be useful tool. You can use it for profiling your programs and finding performance bottlenecks and track down memory management and threading bugs. You could also just switch from programming in C to programming in Rust of course, which is memory and thread safe by design.
Image

Post Reply

Return to “Scripts & Bash”