unit 8
play

Unit 8 Compiling C/C++ Code Debugging Programs 2 COMPILATION 3 - PowerPoint PPT Presentation

1 Unit 8 Compiling C/C++ Code Debugging Programs 2 COMPILATION 3 Editors "Real" developers use editors designed for writing code No word processors!! You need a text editor to write your code Eclipse, Sublime, MS


  1. 1 Unit 8 Compiling C/C++ Code Debugging Programs

  2. 2 COMPILATION

  3. 3 Editors • "Real" developers use editors designed for writing code – No word processors!! • You need a text editor to write your code – Eclipse, Sublime, MS Visual Code, Emacs, Atom, and many others • These often have handy functions for commenting, indenting, checking matching braces ({..}) etc.

  4. 4 Compilers • Several free and commercial compilers are available – g++: – clang++ – XCode – MS Visual Studio • Several have "integrated" editors, debuggers and other tools and thus are called IDE's (Integrated Development Environments)

  5. 5 Using the Command Line • While GUIs are nice, we often have more control when using the command line interface (i.e. the terminal) • Linux (the OS used by Vocareum and in Terminal Icon CS 103, 104, etc.) has a rich set of command line utilities (Mac & Windows do to though Windows uses different names for the utilities) • We can navigate the file system (like you Linux Terminal View would with Explorer or Finder), start programs (double-clicking an icon), and much more by simply typing commands Vocareum Terminal View

  6. 6 Software Process Std C++ & Other Libraries #include <iostream> using namespace std; 1110 0010 0101 1001 int main() 0110 1011 0000 1100 Load & { int x = 5; 0100 1101 0111 1111 g++ cout << "Hello" 1010 1100 0010 1011 Execute << endl; 0001 0110 0011 1000 cout << "x=" << x; return 0; Executable Compiler Binary Image } Note: Most documentation ("test") C++ file(s) and books use $ as a Input file Output file (test.cpp) placeholder for the command (source code) (binary executable) line prompt. $ subl test.cpp & $ subl test.cpp & $ subl test.cpp & $ g++ – g – Wall test.cpp – o test $ g++ – g – Wall test.cpp – o test or $ ./test $ make test Edit & write Compile & fix compiler Load & run the 1 2 3 code errors executable program

  7. 7 g++ Options • Most basic usage – g++ cpp_filenames – Creates an executable a.out • Options – -o => Specifies output executable name (other than default a.out) – -g => Include info needed by debuggers like gdb, kdbg, etc. – -Wall => show all warnings • Most common usage form: – $ g++ -g -Wall hw8.cpp -o hw8

  8. 8 Listing Files (Folder Contents) • In Mac/Linux, to view the files in a folder, just type ls (stands for list) Source code file Executable

  9. 9 Running the Program • First ensure the program compiles – $ g++ -g -Wall hw8.cpp -o hw8 • Then run the program by preceding the executable name with ./ – $ ./hw8

  10. 10 (Part 2 in a few weeks) DEBUGGING – PART 1

  11. 11 Bugs • The original "bug"

  12. 12 Step 1: Review your Own Code • Rubber Duck Debugging : Reference from an anecdote from a book, "The Pragmatic Programmer", that has become popular • Idea : Explain your code line by line to yourself or some other "object" – Note: Commenting your code is a way to do this

  13. 13 Step 2: Test Cases & Expected Outputs • Determine input test case(s) that will exercise various parts of your code – Each if/else block – When the loop executes 0, 1, or more times • Determine the expected output – You cannot effectively debug without an expectation of the right output so you know when the program is working

  14. 14 Step 3: Tracing • Use one of your input #include <iostream> using namespace std; scenarios that is not int main() { int ones=0, tens=0; working and trace int num; while(ones != 2 || tens != 2){ cout << "Enter a num: " << endl; the execution of your cin >> num; if(num < 10){ code by hand ones++; ones tens num } else { – Make a table of tens++; } if(num < 0){ variables and walk break; } the code line by line } cout << "ones: " << ones; cout << " tens: " << tens << endl; return 0; }

  15. 15 Step 4: Print Statements / Narration • Now that you know what to expect, the most common and easy way is to find the error is to add print statements that will "narrate" where you are and what the variable values are • Be a detective by narrowing down where the error is – Put a print statement in each 'for', 'while', 'if' or 'else' block …this will make sure you are getting to the expected areas of your code – Then print variable values so you can see what data your program is producing

  16. 16 Tips • Don't write the entire program all at once • Write a small portion, compile and test it – Write the code to get the input values, add some couts to print out what you got from the user, and make sure it is what you expect – Write a single loop and test it before doing nested loops • Once one part works, add another part and test it

  17. 17 Vocareum Exercises 1 • diff1 – Count how many consecutive values differ by 1 • atleast2 – Input numbers until we have at least 2 from the range 0-9 and >=10 but stop immediately if the user enters a negative number • craps – 1. The player rolls 2 dice. – 2. If the sum of the dice is 7 or 11 the player wins their bet and the turn continues (go back to step 1). – 3. If the sum of the dice is 2, 3, or 12 the player loses their bet, but the turn continues (go back to step 1). – 4. If the sum is any other number (besides 2, 3, 7, 11, or 12) then that value is known as the point number and play continues. – 5. The player rolls the dice until... • a. The sum of the dice is 7 in which case the player loses their bet and the turn ENDS • b. The sum of the dice is the same as the point value in which case the player wins their bet and the turn continues, starting over at step 1.

  18. 18 LINUX AND NAVIGATING FILE SYSTEMS

  19. 19 Linux • Based on the Unix operating system • Developed as an open-source ("free") alternative by Linux Torvalds and several others starting in 1991 • Originally only for Intel based processors but has now been ported to other platforms (i.e. ARM processors in your phone, etc.) • Commonly used in industry and in embedded devices

  20. 20 Using the Command Line • While it has a GUI interface like your Mac or Windows PC much of its power lies in its rich set of utilities that are most easily run Terminal Icon at the command line (aka command prompt or terminal) • Here we can navigate the file system (like you would with Explorer or Linux Terminal View Finder), start programs (double- clicking an icon), and much more by simply typing commands Vocareum Terminal View

  21. 21 Navigating the File System • A file system has – Folders (directories) – Files • They are organized in a hierarchy • Everything we can do with a GUI we can do at the command line

  22. 22 Some Basic Commands • Here are some helpful commands to use in Linux at the command prompt Command ls List (see) all files in the current folder Present working directory shows the current folder pwd location of the terminal cd dirname Change directory to a new folder cp srcfiles dest Copy file(s) to a new location Move/rename files to a new name/location mv srcfile dest rm srcfiles Remove files from the current folder mkdir dirname Make directory / create a new folder rmdir dirname Remove directory / delete a folder (must be empty first)

  23. 23 Directory Structure Ex. 1 • Each circle is a directory / • Each name in the box is a file home • Starting from your home (e.g. 'mark') directory/folder… you start here mark • Use cd to change directories (folders) Desktop Documents – cd Desktop – cd cs102 cs102 other – cd hw7 src • Or go multiple folders at a time hw7 test2.h – cd Desktop/cs102/hw7 test2.cpp hw7a.cpp hw7b.cpp

  24. 24 Directory Structure Ex. 2 • To go up a level use / – cd .. • To go up 2 levels use home – cd ../.. you start here • Let's go one level to 'cs102' mark – cd .. • Now make a directory Desktop Documents – mkdir hw8 cs102 other src Shortcuts: . = Current directory hw7 hw8 .. = Parent directory (up one) test2.h ~ = Home directory test2.cpp * = Wildcard to match filenames hw7a.cpp Unix commands: hw7b.cpp pwd = Print current working dir

  25. 25 Directory Structure Ex. 3 • Let's say we want to start a / new lab with a copy of our old work and just modify it. home Let's copy our work you start here – Recall I'm in cs102 folder mark currently – cp hw7/* hw8/ Desktop Documents cs102 other src Shortcuts: . = Current directory hw8 hw7 .. = Parent directory (up one) test2.h ~ = Home directory test2.cpp * = Wildcard to match filenames hw7a.cpp hw7a.cpp Unix commands: hw7b.cpp hw7b.cpp pwd = Print current working dir

  26. 26 Directory Structure Ex. 4 • Let's now go into the test / folder – cd test home • Now rename the hw7a.cpp to you start here hw8.cpp mark – mv hw7a.cpp hw8.cpp • Now delete the hw7b.cpp file Desktop Documents – rm hw7b.cpp • Remember, you can see all the cs102 other src files in a folder by typing – ls • hw8 hw7 You can see what test2.h folder/directory you are in by test2.cpp typing hw7a.cpp hw7a.cpp – pwd hw7b.cpp hw7b.cpp

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend