1 / 6

SEEM3460 Tutorial

SEEM3460 Tutorial. Compiling and Debugging C programs. 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

afia
Download Presentation

SEEM3460 Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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”

  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.

  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

  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.

  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

More Related