Unformatted text preview:

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 24 2 Stages of Compilation What are the stages of the compilation of a program Department of Computer Science University of San Francisco p 2 24 3 Stages of Compilation What are the stages of the compilation of a program Lexing separate source code into tokens Parsing evaluate statements and generate assembly code Assembly transform assembly code into binary code Linking Multiple object files are merged into a single executable Department of Computer Science University of San Francisco p 3 24 4 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 a single address space Builds an executable with a single main entry point Department of Computer Science University of San Francisco p 4 24 5 Stages of Compilation The C compiler adds another step at the beginning of the process 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 24 6 Included headers In C headers are included directly in the source code at the very first stage of compilation This can sometimes lead to strange compiler errors File A includes file B and file B includes file A Department of Computer Science University of San Francisco p 6 24 7 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 the string ARRAYSIZE with the string 100 Department of Computer Science University of San Francisco p 8 24 9 Macros We can also do this syntactic replacement with blocks of code define SWAP a b int temp a a b b temp int main void int x 10 int y 20 printf d d x y SWAP x y printf d d x y All occurrences of SWAP are replaced with the corresponding code Department of Computer Science University of San Francisco p 9 24 10 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 overhead from a function call Disadvantages Can be difficult to debug Department of Computer Science University of San Francisco p 10 24 11 The Symbol table Normally gcc constructs a symbol table during the compilation process This is used to map variable names to addresses Typically the symbol table is discarded after object files are generated 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 24 12 GDB GDB is the GNU debugger This is very helpful for debugging C programs Same features as the eclipse debugger Break run list print variables step Department of Computer Science University of San Francisco p 12 24 13 GDB break stop at a particular line a particular function Department of Computer Science University of San Francisco p 13 24 14 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 24 15 GDB print show the current value of a variable list show the current lines in the program Department of Computer Science University of San Francisco p 15


View Full Document

USF CS 112 - Compiling and debugging

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Loading Unlocking...
Login

Join to view Compiling and debugging and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Compiling and debugging and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?