1 / 55

T O O L S

P. r. o. g. r. a. m. m. i. n. g. T O O L S. Topics. Programming in C The make utility Source Code Management. Programming in C. Why C? Portable language Easy access to system resources via system calls Memory Files System Functions

kiele
Download Presentation

T O O L S

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. P r o g r a m m i n g T O O L S

  2. Topics • Programming in C • The make utility • Source Code Management

  3. Programming in C • Why C? • Portable language • Easy access to system resources via system calls • Memory • Files • System Functions • Available through a variety of libraries

  4. Programming in C • C Libraries • I/O • Mathematics • Graphics

  5. Programming in C • The Process • Analyze • Understand the problem • Create the problem algorithms • Coding • Translate problem into C language • Compiling • Translate C code into object code • Linking • Link edit object code into executable code

  6. Programming in C • Analyze this Write a program (DisplayIt) that will prompt the user for their name and then clear the terminal and write their name in the middle of the cleared terminal and display the prompt at the bottom of the terminal.

  7. Programming in C • Algorithms • Prompt the user for name. • Clear the terminal. • Write enough blank lines to position to middle of screen. • Write enough spaces to center name • Write name • Write enough blank lines to position to bottom.

  8. Programming in C • Coding • Parts of a C program • Comments – provide information// /*… … … … */ • Declarations – state what is known string userName; • Functions – provide execution steps void main()

  9. Programming in C • C statements • Data types • Standard • User defined • Identifiers • Must start with a letter or underscore • Expressions • Any valid combination of operators & operands

  10. Programming in C • C statements • Preprocessor directives • Identified by a # followed by a preprocessor keyword • Used to provide compiler access to system resources, macros and definitions • #include <iostream> • #define tabsize 4

  11. Programming in C • Coding //****************************************** //* * //* * //* * //* * //* * //****************************************** This program will prompt the user for their name and display it in the center of the terminal screen Flowerbox

  12. Programming in C • Coding #include <iostream> #include <iomanip> #include <string> #define lineWidth 80 #define screenLength 24 Directives using namespace std; string getName(); FunctionalPrototype

  13. Programming in C • Call the function that willPrompt the user for their name. void main() { int i; string userName; userName=getName(); • Clear the terminal. system(“clear"); • Write enough blank lines to position to middle of screen. for(i =1; i<screenLength/2; i++) cout << endl;

  14. Programming in C • Write enough spaces to center name for(i =1; i<(lineWidth-userName.length())/2; i++) cout << " "; • Write the name cout << userName << endl; • Write enough blank lines to position to bottom. for(i =1; i<screenLength/2; i++) cout << endl; return; }

  15. Programming in C Read the users name from the standard input device string getName() { string temp; //Declare a holding area cout << “Enter your name \n”; cin >> temp; //Populate with name return temp; //Return name to caller }

  16. Programming in C • Compiling Pre-processor Inserts pre-coded files to match system hardware Compiler Translates C Source code to Assembly language Assembler Assembles Source code into Object code Linkage Editor Links Object code modules and libraries into an Executable binary file

  17. Programming in C • Compiling … Linux1]$ cc[-options] file-list [-larg] Or … Linux1]$gcc[-options] file-list [-larg] Or … Linux1]$g++[-options] file-list [-larg ] Translate from source code to object code

  18. Programming in C • Compiling -c Compile only no link edit -o Output file name -g Insert debug information -S Supress assembly and link -E Everything is supressed (pre-process only) The CommonOptions

  19. Programming in C • Linking … Linux1]$ g++ -g DisplayIt.cpp -o/bin/DisplayIt Link object code into executable code

  20. The make Utility • Construction requires a specific order to be followed. The House. • 1st build the foundation • 2nd build the frame • 3rd build interior • 4th build trim.

  21. The make Utility • Identifies the dependencies in constructing complex programs • Specifies the order of construction • Includes compilation options • Includes object code (non-executable) • Includes binary code (executable)

  22. The make Utility • The makefile file • Target • The name of the file that is dependent on the pre-requisite list • Pre-requisite list • The list of files who must be present and current for the target to be current.

  23. The make Utility • The makefile file • Construction commands • The commands to execute to produce the target. • Example follows:

  24. The make Utility num.h table.h Text files Headers form.h size.c length.c Source size.o length.o Object form Executable

  25. The make Utility num.h table.h Text files Headers form.h size.c length.c Source size.o length.o Object form Executable

  26. The make Utility • The makefile file form: size.o length.o g++ -o form size.o length.o size.o: size.c form.h g++ -c size.c length.o: length.c form.h g++ -c length.c form.h: num.h table.h cat num.h table.h > form.h

  27. The make Utility num.h table.h Text files Headers form.h size.c length.c Source size.o length.o Object form Executable

  28. The make Utility • The makefile file form: size.o length.o g++ -o form size.o length.o size.o: size.c form.h g++ -c size.c length.o: length.c form.h g++ -c length.c form.h: num.h table.h cat num.h table.h > form.h

  29. The make Utility num.h table.h Text files Headers form.h size.c length.c Source size.o length.o Object form Executable

  30. The make Utility • The makefile file form: size.o length.o g++ -o form size.o length.o size.o: size.c form.h g++ -c size.c length.o: length.c form.h g++ -c length.c form.h: num.h table.h cat num.h table.h > form.h

  31. The make Utility • make assumptions • .o – files have corresponding source files • The source suffix will determine which compile to invoke. • make is lazy it will only compile what it thinks is needed. • Based on the timestamp of files

  32. Debugging Programs STOP!

  33. Debugging Programs • Compiler Warnings • Anything other than 0 errors/warnings is sloppy coding and potential bugs. • The insertion principle or cout is your friend. • cout <<“*** I am about to enter***\n”; • cout <<“*** I am about to exit***\n”; • cout << “*** data value=“<<x<<‘\n’;

  34. Debugging Programs • Symbolic Debuggers allow you to: • Start your program, specifying anything that might affect its behavior. • Make your program stop on specified conditions. • Examine what has happened, when your program has stopped. • Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

  35. Debugging Programs • Debugger and program execution • Programs must be compiled with the –g option for the debugger to use source code line numbers and variable definitions. • Programs are executed within a debugging session.

  36. Debugger Execution • gdbpgmname [core|PID] • Invokes the debugger and loads the binary file • Commands • list – displays 10 lines of source code • break – set a stop point during execution • run – executes program until breakpoint is reached

  37. Debugger Execution • Commands • print – displays the value of a variable at the breakpoint • bt– backtrace displays all of the entries in the execution stack. • up– moves your view of the program up one level in the stack • down – moves your view of the program down one level in the stack

  38. Debugger Execution • Commands • step – steps through one instruction • next – steps through to the next instruction of this function • continue – continue execution • set – changes the value of a variable • call – invokes a program function

  39. Debugger Example …]$ gbd –silent stars (gdb) list 1,22 1 #include <iostream> 2 using namespace std; 3 int count=0; 4 int count2=1; 5 char star ='@'; 6 7 void printstars(int); 8 9 int main() 10 { 11 12 while (count< 1 || count > 23) 13 { 14 cout <<"enter a number from 1 through 23"<<endl;15 cin >> count; 16 cout << endl; 17 } 18 19 while (count2 < count) 20 { 21 printstars(count2); 22 count2++; (gbd)

  40. Debugger Example (gdb) list 19,40 19 while (count2 < count) 20 { 21 printstars(count2); 22 count2++; 23 } 24 25 while (count2 > 0) 26 { 27 printstars(count2); 28 count2--; 29 } 30 31 return 0; 32 } 33 34 void printstars(int num) 35 { 36 int x=0; 37 while (x < num) 38 { 39 cout << '*'; 40 x++; (gdb) breakpoint 21Breakpoint 1 at 0x8048774: file /home/user/C++Src/Stars.cpp, line 21.

  41. Debugger Example (gdb) run Starting program: /home/jurrutia/bin/Stars enter a number from 1 through 23 5 Breakpoint 1, main () at /home/jurrutia/C++Src/Stars.cpp:21 warning: Source file is more recent than executable. 21 printstars(count2); (gdb) print count $1 = 5 (gdb) print count2$2 = 1 (gdb) c Continuing. *........* Breakpoint 1, main () at /home/jurrutia/C++Src/Stars.cpp:2121 printstars(count2); (gdb)

  42. Debugger Example (gdb) end This command cannot be used at the top level.(gdb) clear Deleted breakpoint 1 (gdb) c Continuing. **......** ***....*** ****..**** ********** ****..**** ***....*** **......** *........* Program exited normally. (gdb) q …@linux1 bin]$

  43. System Calls • Direct invocation of kernel functions • fork() • exec() • wait() • exit() • kill() • Open() • Read() • Write() • Close()

  44. RCS - CVS • Revision Control System • Source code changes are managed programmatically to avoid undoing what has already been done. • Track all changes to between revisions • 4 utilities control almost everything • ci – co – rlog – rcsdiff

  45. RCS - CVS • RCS traces all changes between a previous document and the current document and records those changes as revisions. • Each revision is numbered • The original document is 1.1 • Each revision is incremented by 1

  46. RCS Check-In • ci [-options] file list • Used to create or update a RCS record • -l– Lock the source to others • -u– Unlock the source to others • -r– Revision number

  47. RCS Check - Out • co [-options] file list • Used to create source files for viewing or updating • -l– Lock the source so I can change • -u– Unlock the source for viewing • -r– Revision number to check-out

  48. RCS rlog • rlog [-options] file list • Used to view the changes made to a file. • -r– Revision number to retrieve

  49. RCS rcsdiff • rcsdiff [-options] file list • Used to view the differences between two revisions of source. • -r– Revision number to compare

  50. Concurrent Version System • CVS works the same way as RCS but provides additional structure and control. • Invokes RCS functions • CVS provides self-documenting features for utilities in the system.

More Related