Compilation and Debugging in C: Stages of Compilation and Use of GDB, Study notes of Computer Science

This document from the university of san francisco's department of computer science explains the stages of compilation of a c program, including preprocessing, lexing, parsing, assembly, and linking. It also covers the use of the gnu debugger (gdb) for debugging compiled c programs, including setting breakpoints, running, continuing execution, stepping through code, and printing variables and showing current lines.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-fdw-1
koofers-user-fdw-1 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Intro to Programming II
Compiling and debugging
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science University of San Francisco p. 1/??
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Compilation and Debugging in C: Stages of Compilation and Use of GDB and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II

Compiling and debugging

Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science — University of San Francisco – p. 1/

Stages of Compilation

  • What are the stages of the compilation of a program? Department of Computer Science — University of San Francisco – p. 2/

Example

  • Assume we start with two source files: file1.c and file2.c - First, each file is parsed and compiled separately. - The linker then builds a single executable ◦ Resolves all function calls and variable references to asingle address space. ◦ Builds an executable with a single “main” entry point. Department of Computer Science — University of San Francisco – p. 4/

Stages of Compilation

  • The C compiler adds another step at the beginning of theprocess. - This step is called preprocessing. - The preprocessor does three things: ◦ Strips out comments. ◦ “Pastes in” included headers. ◦ Expands macros. Department of Computer Science — University of San Francisco – p. 5/

Macros

  • The C preprocessor also uses a #define directive. - This is used to: ◦ Define constants ◦ Define macros: small chunks of code. Department of Computer Science — University of San Francisco – p. 7/

24-8:

Defining constants

#define ARRAYSIZE 100 int arr[ARRAYSIZE];

  • The preprocessor replaces ARRAYSIZE with 100 - Note: this is NOT a variable - the preprocessor just replaces thestring ’ARRAYSIZE’ with the string ’100’ Department of Computer Science — University of San Francisco – p. 8/

Macros

  • Note that SWAP is not a function. ◦ Code is just cut-and-pasted. ◦ No checking of argument types ◦ No return types - Advantages: useful for ’quick and dirty’ operations, no overheadfrom a function call. - Disadvantages: Can be difficult to debug. Department of Computer Science — University of San Francisco – p. 10/

The Symbol table

  • Normally, gcc constructs a symbol table during the compilationprocess. - This is used to map variable names to addresses. - Typically, the symbol table is discarded after object files aregenerated. - Compiling with the -g option retains the symbol table. - This allows us to run the debugger. Department of Computer Science — University of San Francisco – p. 11/

GDB

  • break - stop at: ◦ a particular line ◦ a particular function. Department of Computer Science — University of San Francisco – p. 13/

GDB

  • run - start a program - continue - continue execution from the breakpoint. - step - move to the next line of the program Department of Computer Science — University of San Francisco – p. 14/