1 © 2000-2002 John Urrutia. All rights reserved. Programming.

55
1 © 2000-2002 John Urrutia. All rights reserved. Progra m m ing

Transcript of 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

Page 1: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

1© 2000-2002 John Urrutia. All rights reserved.

Programming

Page 2: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

2© 2000-2002 John Urrutia. All rights reserved.

Topics

Programming in C

The make utility

Source Code Management

Page 3: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

3© 2000-2002 John Urrutia. All rights reserved.

Programming in CWhy C?

Portable language

Easy access to system resources via system callsMemory Files

System FunctionsAvailable through a variety of libraries

Page 4: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

4© 2000-2002 John Urrutia. All rights reserved.

Programming in CC Libraries

I/O

Mathematics

Graphics

Page 5: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

5© 2000-2002 John Urrutia. All rights reserved.

Programming in CThe Process

AnalyzeUnderstand the problemCreate the problem algorithms

CodingTranslate problem into C language

CompilingTranslate C code into object code

LinkingLink edit object code into executable code

Page 6: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

6© 2000-2002 John Urrutia. All rights reserved.

Programming in CAnalyze

thisWrite 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.

Page 7: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

7© 2000-2002 John Urrutia. All rights reserved.

Programming in CAlgorithm

s1. Prompt the user for name.2. Clear the terminal.3. Write enough blank lines to

position to middle of screen.4. Write enough spaces to center

name5. Write name6. Write enough blank lines to

position to bottom.

Page 8: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

8© 2000-2002 John Urrutia. All rights reserved.

Programming in CCoding

Parts of a C programComments – provide information

///*… … … … */

Declarations – state what is knownstring userName;

Functions – provide execution stepsvoid main()

Page 9: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

9© 2000-2002 John Urrutia. All rights reserved.

Programming in CC statements

Data typesStandardUser defined

IdentifiersMust start with a letter or underscore

ExpressionsAny valid combination of operators &

operands

Page 10: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

10© 2000-2002 John Urrutia. All rights reserved.

Programming in CC 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

Page 11: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

11© 2000-2002 John Urrutia. All rights reserved.

Programming in CCodin

g//******************************************//* *//* *//* *//* *//* *//******************************************

This program will prompt the userfor their name and display it inthe center of the terminal screen

Flowerbox

Page 12: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

12© 2000-2002 John Urrutia. All rights reserved.

Programming in CCodin

g#include <iostream>#include <iomanip>#include <string>#define lineWidth 80#define screenLength 24

Directives

using namespace std;

string getName(); FunctionalPrototype

Page 13: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

13© 2000-2002 John Urrutia. All rights reserved.

Programming in C

1. Call the function that willPrompt the user for their name.

void main(){

int i;string userName;userName=getName();

2. Clear the terminal.system(“clear");

3. Write enough blank lines to position to middle of screen.for(i =1; i<screenLength/2; i++)

cout << endl;

Page 14: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

14© 2000-2002 John Urrutia. All rights reserved.

Programming in C

4. Write enough spaces to center name

for(i =1; i<(lineWidth-userName.length())/2; i++) cout << " ";

5. Write the namecout << userName << endl;

6. Write enough blank lines to position to bottom.

for(i =1; i<screenLength/2; i++)cout << endl;

return;}

Page 15: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

15© 2000-2002 John Urrutia. All rights reserved.

Programming in C

Read the users name from the standard input device

string getName(){

string temp; //Declare a holding areacout << “Enter your name \n”;cin >> temp; //Populate with namereturn temp; //Return name to caller

}

Page 16: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

16© 2000-2002 John Urrutia. All rights reserved.

Programming in CCompilin

g Pre-processorInserts pre-coded files to match system hardware

Compiler

Assembler

Linkage Editor

Translates C Source code to Assembly language

Assembles Source code into Object code

Links Object code modules and libraries into an Executable binary file

Page 17: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

17© 2000-2002 John Urrutia. All rights reserved.

Programming in CCompilin

g

Translate from source code

to object code

… Linux1]$ cc [-options] file-list [-larg ]Or

… Linux1]$ gcc [-options] file-list [-larg ]Or

… Linux1]$ g++ [-options] file-list [-larg ]

Page 18: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

18© 2000-2002 John Urrutia. All rights reserved.

Programming in CCompilin

g

The CommonOptions

-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)

Page 19: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

19© 2000-2002 John Urrutia. All rights reserved.

Programming in CLinking

Link object code

into executable code… Linux1]$ g++ -g DisplayIt.cpp -o /bin/DisplayIt

Page 20: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

20© 2000-2002 John Urrutia. All rights reserved.

The make UtilityConstruction requires a specific order

to be followed. The House.1st build the foundation

2nd build the frame

3rd build interior

4th build trim.

Page 21: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

21© 2000-2002 John Urrutia. All rights reserved.

The make UtilityIdentifies the dependencies in

constructing complex programs

Specifies the order of constructionIncludes compilation options

Includes object code (non-executable)

Includes binary code (executable)

Page 22: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

22© 2000-2002 John Urrutia. All rights reserved.

The make Utility

The makefile fileTarget

The name of the file that is dependent on the pre-requisite list

Pre-requisite listThe list of files who must be present and

current for the target to be current.

Page 23: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

23© 2000-2002 John Urrutia. All rights reserved.

The make Utility

The makefile fileConstruction commands

The commands to execute to produce the target.

Example follows:

Page 24: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

24© 2000-2002 John Urrutia. All rights reserved.

The make Utility

num.h table.h

form.h

size.c length.c

size.o length.o

form

Text files

Headers

Source

Object

Executable

Page 25: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

25© 2000-2002 John Urrutia. All rights reserved.

The make Utility

num.hnum.h table.htable.h

form.hform.h

size.c length.c

size.o length.o

form

Text files

Headers

Source

Object

Executable

Page 26: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

26© 2000-2002 John Urrutia. All rights reserved.

The make UtilityThe makefile file

form: size.o length.og++ -o form size.o length.o

size.o: size.c form.hg++ -c size.c

length.o: length.c form.hg++ -c length.c

form.h: num.h table.hcat num.h table.h > form.h

Page 27: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

27© 2000-2002 John Urrutia. All rights reserved.

The make Utility

size.csize.c length.clength.c

size.osize.o length.olength.o

form

Text files

Headers

Source

Object

Executable

num.h table.h

form.h

Page 28: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

28© 2000-2002 John Urrutia. All rights reserved.

The make UtilityThe makefile file

form: size.o length.og++ -o form size.o length.o

size.o: size.c form.hg++ -c size.c

length.o: length.c form.hg++ -c length.c

form.h: num.h table.hcat num.h table.h > form.h

Page 29: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

29© 2000-2002 John Urrutia. All rights reserved.

The make Utility

num.h table.h

form.h

size.c length.c

size.osize.o length.olength.o

formform

Text files

Headers

Source

Object

Executable

Page 30: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

30© 2000-2002 John Urrutia. All rights reserved.

The make UtilityThe makefile file

form: size.o length.og++ -o form size.o length.o

size.o: size.c form.hg++ -c size.c

length.o: length.c form.hg++ -c length.c

form.h: num.h table.hcat num.h table.h > form.h

Page 31: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

31© 2000-2002 John Urrutia. All rights reserved.

The make Utility make assumptions

.o – files have corresponding source filesThe 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

Page 32: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

32© 2000-2002 John Urrutia. All rights reserved.

Debugging Programs

STOP!

Page 33: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

33© 2000-2002 John Urrutia. All rights reserved.

Debugging ProgramsCompiler 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’;

Page 34: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

34© 2000-2002 John Urrutia. All rights reserved.

Debugging ProgramsSymbolic 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.

Page 35: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

35© 2000-2002 John Urrutia. All rights reserved.

Debugging ProgramsDebugger 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.

Page 36: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

36© 2000-2002 John Urrutia. All rights reserved.

Debugger Executiongdb pgmname [core|PID]

Invokes the debugger and loads the binary file

Commandslist – displays 10 lines of source codebreak – set a stop point during executionrun – executes program until

breakpoint is reached

Page 37: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

37© 2000-2002 John Urrutia. All rights reserved.

Debugger ExecutionCommands

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

Page 38: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

38© 2000-2002 John Urrutia. All rights reserved.

Debugger ExecutionCommands

step – steps through one instruction

next – steps through to the nextinstruction of this

function

continue – continue execution

set – changes the value of a variable

call – invokes a program function

Page 39: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

39© 2000-2002 John Urrutia. All rights reserved.

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)

Page 40: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

40© 2000-2002 John Urrutia. All rights reserved.

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.

Page 41: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

41© 2000-2002 John Urrutia. All rights reserved.

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)

Page 42: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

42© 2000-2002 John Urrutia. All rights reserved.

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]$

Page 43: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

43© 2000-2002 John Urrutia. All rights reserved.

System Calls

fork()

exec()

wait()

exit()

kill()

Direct invocation of kernel functions

Open()

Read()

Write()

Close()

Page 44: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

44© 2000-2002 John Urrutia. All rights reserved.

RCS - CVSRevision 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 everythingci – co – rlog – rcsdiff

Page 45: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

45© 2000-2002 John Urrutia. All rights reserved.

RCS - CVSRCS traces all changes between a

previous document and the current document and records those changes as revisions.

Each revision is numberedThe original document is 1.1

Each revision is incremented by 1

Page 46: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

46© 2000-2002 John Urrutia. All rights reserved.

RCS Check-Inci [-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

Page 47: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

47© 2000-2002 John Urrutia. All rights reserved.

RCS Check - Outco [-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

Page 48: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

48© 2000-2002 John Urrutia. All rights reserved.

RCS rlogrlog [-options] file list

Used to view the changes made to a file.

-r – Revision number to retrieve

Page 49: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

49© 2000-2002 John Urrutia. All rights reserved.

RCS rcsdiffrcsdiff [-options] file list

Used to view the differences between two revisions of source.

-r – Revision number to compare

Page 50: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

50© 2000-2002 John Urrutia. All rights reserved.

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.

Page 51: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

51© 2000-2002 John Urrutia. All rights reserved.

Why CVS ?CVS is decentralized so a user checks out

files/directories from the repository and have his own separate stable source directory tree.

CVS can "STAMP" releases of an entire project source tree.

CVS can enable concurrent editing of files. CVS can be greatly customized to enable strong

locking of files via shell scripts or PERL scripts. CVS supports weak locking with the command 'cvs watches' and also no locking permitting concurrent editing of files.

Page 52: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

52© 2000-2002 John Urrutia. All rights reserved.

Why not CVS?Needs a little more administration than

RCS.

Very complex and sophisticated system."State of the Art" technology.Developed with 20 to 30 years of research.It’s still evolving!!

Has a large number of commands and command options, hence a steeper learning curve for beginners.

Page 53: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

53© 2000-2002 John Urrutia. All rights reserved.

CVS Commandscheckout – Always makes copies of

source project into your specified working directory.Pulls most recently committed version.

Creates any tree structures needed.

Page 54: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

54© 2000-2002 John Urrutia. All rights reserved.

CVS Commandsupdate – Applies any changes

made by other users to your version of code.Notifies about overlapping changes.

add – Adds new files to the project.

remove – Removes files or directories from a project.

Page 55: 1 © 2000-2002 John Urrutia. All rights reserved. Programming.

55© 2000-2002 John Urrutia. All rights reserved.

CVS Commandscommit – Updates the repository

project with your project changes.

diff – Displays differences between Repository and working files

Revisions of a file in the repository