Thread: debugging a C code on Linux OS

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Red face debugging a C code on Linux OS

    Hi,

    I have a code that I'd like to debugg it.

    Question
    1 - What is the correct command line to use the debugger ?
    2 - Do I need to install additional software for the debugger ?
    if yes what it's name?



    • I use ububtu 12.4
    • compiling command: gcc abc.c -lpthread -o abc

  2. #2
    spaghetticode
    Guest
    gdb is kind of the standard debugger under linux operating systems, just like gcc is kind of the standard compiler. If not already installed, install it via

    Code:
    sudo apt-get install gdb
    For reference on usage see the manpage, command:

    Code:
    man gdb

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Compile your code like this to make debugging actually worthwhile (that is, enabling debug symbols and generating warnings, which you should fix first):

    Code:
    gcc -ggdb3 -Wall -pedantic -lpthread -o abc abc.c
    -ggdb3 enables the debug symbols, the other settings are for warning generation. Depending on how you wrote your code you may need to add --std=c99 or --std=c11 to the options.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    The first question doesn't seem to be answered yet, but - as mentioned - gdb is the tool to use and the compilation needs to have debugging symbols options enabled.

    The command is simply "gdb <executable>" in other words, you perform gdb on the compiled version of the code. gdb will know which c source file it came from and present you with the text. Once your in gdb typing "run" will run the code within the debugger, but you will need to set a breakpoint if you want it to stop somewhere within the code.

    gdb can be somewhat stark, if you're a vim user "cgdb" is a good, more visual way to handle it. Emacs on its own is already good with gdb .. Gdb is very powerful, but learnign it is a very good investment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debugging C code
    By MASX in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 05:45 AM
  2. Help with Debugging Code
    By husslela2 in forum C Programming
    Replies: 14
    Last Post: 03-24-2010, 02:56 PM
  3. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  4. WinGDB - Linux debugging under Visual Studio
    By WinGDB in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 07-01-2009, 03:05 PM
  5. Debugging on Linux?
    By trlpkguy in forum Linux Programming
    Replies: 10
    Last Post: 10-23-2006, 12:56 PM