Programming and Debugging C on Linux

This “how-to” covers programming and debugging on linux without the use of an IDE and mainly using the console.

Writing programs on Linux can seem daunting at first, especially if your just using a simple editor like nano and manually compiling files (like how i started off doing).
But there are many things that can help and make things far more productive.

First thing is to use a decent editor that can have multiple files open at once like emacs, gedit or similar.
Second if your manually compiling use make to compile your files.

Compiling using Make

Make is excellent at saving you time when you need to recompile your project, mainly because it only recomiles what is neccessary. A makefile is need in the directory of your source code (called makefile or Makefile) this is read by make when you run it. and is called with make [target] the target is optional, if not given will use the first rule in the makefile.

A simple makefile looks like this:

OBJS = file1.o file2.o file3.o
CCOPTS = -g

prog: $(OBJS)
    $(CC) $(CCOPTS) -o prog $(OBJS)
file1.o:
    $(CC) $(CCOPTS) -c file1.c
file2.o:
    $(CC) $(CCOPTS) -c file2.c
file3.o:
    $(CC) $(CCOPTS) -c file3.c
clean:
    rm prog $(OBJS)

The above makefile will instruct make to compile all the .c files into .o files and compile them into the program prog. each rule is set out as:

target : prerequisites
    command
    command
    ....
^^^^ must be a tab

Make will only perform the command if the prerequisites are newer than the target, ie if you edit one of the source files it will be newer than the target, thus saves you time by not re-compiling things that have not changed since the last time make was run. clean is included because from time to time you may change the compiling options, when you do you will have remove the old files in order to force make to re-compile them with the new settings; run make clean to do this. So now instead of manually re-compiling all your source files, you can just call make.

Debugging using GDB

If like me your used to a development environment like visual studio telling you what youve done wrong trying to debug anything by hand can be a nightmare. Luckly gdb is here to help.
Before you can use gdb effectively you need to re-compile your code with at least the flag -g (if you haven’t already) this will force the compiler to include debugging information in the executable that gdb can use to report where something went wrong.

To debug a program start gdb with the command gdb [yourprogram]. Once running you will see something like this:

GNU gdb 6.6-debian
Copyright (C) 2006 Free Software Foundation, Inc.
...stuff...
(gdb)

Here is a quick list of the commands you can use:

  • run [program args] – use this to run your program as you would at the command line, redirection will also work here
  • quit or q – exit gdb
  • shell [command] – run an external command like make
  • break [lineno|filename:lineno|functionname|file:functionname] – use this to insert a breakpoint, this will cause your program to be paused and you will be brought back to the gdb prompt so you can do stuff
  • clear [breakinfo] – use this to remove a breakpoint
  • delete – delete all breakpoints You can use the following when your program has crashed ot has been paused with a breakpoint
  • delete breakpoints [number] – Delete a particular breakpoint
  • c or continue – continue running the program (if stopped)
  • bt – show the backtrace (the list of function calls that lead to this point)
  • up – prints the stack frame that called the current function.
  • s or step – result the program and stop at the next command
  • kill – kill your program

if you need to recompile it you can use shell make (use file [exename] to reload it into gdb) There are far more commands that can be used, see the gdb manual Here.

Bookmark the permalink.

Leave a Reply

Your email address will not be published.