Download presentation
Presentation is loading. Please wait.
Published byWilliam Jones Modified over 9 years ago
2
1 © 2000-2002 John Urrutia. All rights reserved. Programming
3
2 Topics Programming in C The make utility Source Code Management
4
3 © 2000-2002 John Urrutia. All rights reserved. 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
5
4 © 2000-2002 John Urrutia. All rights reserved. Programming in C C Libraries I/O Mathematics Graphics
6
5 © 2000-2002 John Urrutia. All rights reserved. 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
7
6 © 2000-2002 John Urrutia. All rights reserved. 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.
8
7 © 2000-2002 John Urrutia. All rights reserved. Programming in C Algorithms 1. 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 name 5. Write name 6. Write enough blank lines to position to bottom.
9
8 © 2000-2002 John Urrutia. All rights reserved. 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()
10
9 © 2000-2002 John Urrutia. All rights reserved. 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
11
10 © 2000-2002 John Urrutia. All rights reserved. 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 #define tabsize 4
12
11 © 2000-2002 John Urrutia. All rights reserved. Programming in C Coding //****************************************** //* * //****************************************** This program will prompt the user for their name and display it in the center of the terminal screen Flowerbox
13
12 © 2000-2002 John Urrutia. All rights reserved. Programming in C Coding #include #define lineWidth 80 #define screenLength 24 Directives using namespace std; string getName(); Functional Prototype
14
13 © 2000-2002 John Urrutia. All rights reserved. Programming in C 1. Call the function that will Prompt 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;
15
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 name cout << userName << endl; 6.Write enough blank lines to position to bottom. for(i =1; i<screenLength/2; i++) cout << endl; return; }
16
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 area cout << “Enter your name \n”; cin >> temp;//Populate with name return temp;//Return name to caller }
17
16 © 2000-2002 John Urrutia. All rights reserved. Programming in C Compiling Pre-processor Inserts 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
18
17 © 2000-2002 John Urrutia. All rights reserved. Programming in C Compiling Translate from source code to object code … Linux1]$ cc [- options ] file-list [-l arg ] Or … Linux1]$ gcc [- options ] file-list [-l arg ] Or … Linux1]$ g++ [-options] file-list [-larg ]
19
18 © 2000-2002 John Urrutia. All rights reserved. Programming in C Compiling The Common Options -c C ompile only no link edit -o O utput file name -g Insert debu g information -S Supress assembly and link -E Everything is supressed (pre-process only)
20
19 © 2000-2002 John Urrutia. All rights reserved. Programming in C Linking Link object code into executable code … Linux1]$ g++ -g DisplayIt.cpp -o /bin/DisplayIt
21
20 © 2000-2002 John Urrutia. All rights reserved. The make Utility Construction requires a specific order to be followed. The House. 1 st build the foundation 2 nd build the frame 3 rd build interior 4 th build trim.
22
21 © 2000-2002 John Urrutia. All rights reserved. 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)
23
22 © 2000-2002 John Urrutia. All rights reserved. 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.
24
23 © 2000-2002 John Urrutia. All rights reserved. The make Utility The makefile file Construction commands The commands to execute to produce the target. Example follows:
25
24 © 2000-2002 John Urrutia. All rights reserved. The make Utility num.htable.h form.h size.clength.c size.olength.o form Text files Headers Source Object Executable
26
25 © 2000-2002 John Urrutia. All rights reserved. The make Utility num.h table.h form.h size.clength.c size.olength.o form Text files Headers Source Object Executable
27
26 © 2000-2002 John Urrutia. All rights reserved. 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
28
27 © 2000-2002 John Urrutia. All rights reserved. The make Utility size.c length.c size.o length.o form Text files Headers Source Object Executable num.htable.h form.h
29
28 © 2000-2002 John Urrutia. All rights reserved. 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
30
29 © 2000-2002 John Urrutia. All rights reserved. The make Utility num.htable.h form.h size.clength.c size.o length.o form Text files Headers Source Object Executable
31
30 © 2000-2002 John Urrutia. All rights reserved. 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
32
31 © 2000-2002 John Urrutia. All rights reserved. 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
33
32 © 2000-2002 John Urrutia. All rights reserved. Debugging Programs STOP!
34
33 © 2000-2002 John Urrutia. All rights reserved. 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’;
35
34 © 2000-2002 John Urrutia. All rights reserved. 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.
36
35 © 2000-2002 John Urrutia. All rights reserved. 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.
37
36 © 2000-2002 John Urrutia. All rights reserved. Debugger Execution gdb pgmname [ 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
38
37 © 2000-2002 John Urrutia. All rights reserved. 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
39
38 © 2000-2002 John Urrutia. All rights reserved. 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
40
39 © 2000-2002 John Urrutia. All rights reserved. Debugger Example …]$ gbd –silent stars (gdb) list 1,22 1 #include 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 23) 13 { 14 cout > count; 16 cout << endl; 17 } 18 19 while (count2 < count) 20 { 21 printstars(count2); 22 count2++; (gbd)
41
40 © 2000-2002 John Urrutia. All rights reserved. Debugger Example (gdb) list 19,40 19 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 21 Breakpoint 1 at 0x8048774: file /home/user/C++Src/Stars.cpp, line 21.
42
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:21 21 printstars(count2); (gdb)
43
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]$
44
43 © 2000-2002 John Urrutia. All rights reserved. System Calls fork() exec() wait() exit() kill() Direct invocation of kernel functions Open() Read() Write() Close()
45
44 © 2000-2002 John Urrutia. All rights reserved. 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
46
45 © 2000-2002 John Urrutia. All rights reserved. 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
47
46 © 2000-2002 John Urrutia. All rights reserved. RCS C heck- I n 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
48
47 © 2000-2002 John Urrutia. All rights reserved. RCS C heck - O ut 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
49
48 © 2000-2002 John Urrutia. All rights reserved. RCS rlog rlog [-options] file list Used to view the changes made to a file. -r – Revision number to retrieve
50
49 © 2000-2002 John Urrutia. All rights reserved. RCS rcsdiff rcsdiff [-options] file list Used to view the differences between two revisions of source. -r – Revision number to compare
51
50 © 2000-2002 John Urrutia. All rights reserved. C oncurrent V ersion S ystem 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.
52
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.
53
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.
54
53 © 2000-2002 John Urrutia. All rights reserved. CVS Commands checkout – Always makes copies of source project into your specified working directory. Pulls most recently committed version. Creates any tree structures needed.
55
54 © 2000-2002 John Urrutia. All rights reserved. CVS Commands update – 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.
56
55 © 2000-2002 John Urrutia. All rights reserved. CVS Commands commit – Updates the repository project with your project changes. diff – Displays differences between Repository and working files Revisions of a file in the repository
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.