550 likes | 714 Views
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
E N D
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 • Available through a variety of libraries
Programming in C • C Libraries • I/O • Mathematics • Graphics
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
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.
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.
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()
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
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
Programming in C • Coding //****************************************** //* * //* * //* * //* * //* * //****************************************** This program will prompt the user for their name and display it in the center of the terminal screen Flowerbox
Programming in C • Coding #include <iostream> #include <iomanip> #include <string> #define lineWidth 80 #define screenLength 24 Directives using namespace std; string getName(); FunctionalPrototype
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;
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; }
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 }
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
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
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
Programming in C • Linking … Linux1]$ g++ -g DisplayIt.cpp -o/bin/DisplayIt Link object code into executable code
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.
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)
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.
The make Utility • The makefile file • Construction commands • The commands to execute to produce the target. • Example follows:
The make Utility num.h table.h Text files Headers form.h size.c length.c Source size.o length.o Object form Executable
The make Utility num.h table.h Text files Headers form.h size.c length.c Source size.o length.o Object form Executable
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
The make Utility num.h table.h Text files Headers form.h size.c length.c Source size.o length.o Object form Executable
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
The make Utility num.h table.h Text files Headers form.h size.c length.c Source size.o length.o Object form Executable
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
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
Debugging Programs STOP!
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’;
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.
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.
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
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
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
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)
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.
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)
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]$
System Calls • Direct invocation of kernel functions • fork() • exec() • wait() • exit() • kill() • Open() • Read() • Write() • Close()
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
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
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
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
RCS rlog • rlog [-options] file list • Used to view the changes made to a file. • -r– Revision number to retrieve
RCS rcsdiff • rcsdiff [-options] file list • Used to view the differences between two revisions of source. • -r– Revision number to compare
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.