6
1 SEEM3460 Tutorial Compiling and Debugging C programs

1 SEEM3460 Tutorial Compiling and Debugging C programs

Embed Size (px)

Citation preview

Page 1: 1 SEEM3460 Tutorial Compiling and Debugging C programs

1

SEEM3460 Tutorial

Compiling and Debugging C programs

Page 2: 1 SEEM3460 Tutorial Compiling and Debugging C programs

2

Compiling C programs in Unix Compiler:

cc – the native C compiler under Unix gcc – C compiler under GNU project Usage is the same, gcc is more popular

To compile a C source code file, say example.c, type in: cc example.c gcc example.c The output of both compilers (i.e. the executable) would be “a.out” by

default To override the default executable name, use “-o” flag

cc example.c –o example gcc example.c –o example You can name the executable as .exe or .bin file to remind yourself the

nature of the file One more tip, use “-Wall” to get some warning messages

Warning message usually indicate hidden bugs Try to understand the error messages and warning messages. For other flags, “man cc” or “man gcc”

Page 3: 1 SEEM3460 Tutorial Compiling and Debugging C programs

3

Compiling C programs in Unix If there are so many compilation errors that it

cannot fit into one screen. One way is to compile by the following command:

cc example.c |& more It means that the compilation errors will be

displayed screen by screen. That is, it will display the first screen of errors and wait.

After the user examines the errors, he/she can choose to display the next screen of errors by hitting RETURN or quit by hitting Control-C. Then, the user can go back to modify the source code.

Page 4: 1 SEEM3460 Tutorial Compiling and Debugging C programs

4

Debugging C programs in Unix Mainly 2 ways:

Add printf to trace the bug Use debugger to find out the bug

Debugger lets you to know: What statement or expression did the program crash

on? If an error occurs while executing a function, what line of

the program contains the call to that function, and what are the parameters?

What are the values of program variables at a particular point during execution of the program?

What is the result of a particular expression in a program?

Reference: http://www.cs.princeton.edu/~benjasik/gdb/gdbtut.html

Page 5: 1 SEEM3460 Tutorial Compiling and Debugging C programs

5

Debugging C programs (2) Debuggers available on your Unix workstations:

dbx – work for cc gdb – work for gcc

To use debugger, add “-g” flag when compiling the program

cc –g example.c –o example gcc –g example.c –o example

And then start the debugger by loading the executable: dbx example (for cc) gdb example (for gcc)

Demo on how to use a debugger (using dbx on Lab asg 1)

The demo covers only a few important commands in dbx. For detail tutorial on using dbx and gdb, google for “gdb tutorial” and “dbx tutorial” respectively.

Page 6: 1 SEEM3460 Tutorial Compiling and Debugging C programs

6

Demo commands on dbx(details see pdf tutorial notes) help run rerun trace step trace change function`variable trace in function stop step print whatis whereis quit