Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Similar documents
Dynamic Arrays Makefiles and Mul3ple File Compiles

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Unit 7. 'while' Loops

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Pre- Defined Func-ons in C++ Review for Midterm #1

CS2141 Software Development using C/C++ Compiling a C++ Program

Chapter 3. More Flow of Control

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Lecture 7. Log into Linux New documents posted to course webpage

CSE 143. Programming is... Principles of Programming and Software Engineering. The Software Lifecycle. Software Lifecycle in HW

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Unit 13. Linux Operating System Debugging Programs

File I/O CS 16: Solving Problems with Computers I Lecture #9

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

These are notes for the third lecture; if statements and loops.

1 Unit 8 'for' Loops

CSE 374 Programming Concepts & Tools

Welcome to Solving Problems with Computers I

C++ For Science and Engineering Lecture 2

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

CSCE Practice Midterm. Data Types

Separate Compilation of Multi-File Programs

Software Design and Analysis for Engineers

EECS402 Lecture 02. Functions. Function Prototype

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

CPSC 427: Object-Oriented Programming

Lab 1: First Steps in C++ - Eclipse

Starting to Program in C++ (Basics & I/O)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Using the Command Line

We first learn one useful option of gcc. Copy the following C source file to your

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

do { statements } while (condition);

Computer Science II Lecture 1 Introduction and Background

How to approach a computational problem

CSE 333 Midterm Exam 7/25/16. Name UW ID#

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Lesson 2 Variables and I/O

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter void Functions. Overview. Functions for All Subtasks. void-functions. Using a void-function. void-function Definition

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Local and Global Variables

CIS 130 Exam #2 Review Suggestions

Practicum 5 Maps and Closures

Tutorial 2: Compiling and Running C++ Source Code

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

Binomial pricer (1.1)

CSCE Practice Midterm. Data Types

CS 103 Unit 11. Linked Lists. Mark Redekopp

Classes in C++98 and C++11

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Laboratory 5: Implementing Loops and Loop Control Strategies

C / C++ Coding Rules

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Chapter Four: Loops II

Fundamentals of Structured Programming

CS11 Introduction to C++ Fall Lecture 1

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

2. Functions I: Passing by Value

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

C/C++ Programming. Session 10

Lab: Supplying Inputs to Programs

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

CSCE 206: Structured Programming in C++

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Absolute C++ Walter Savitch

PIC 10A Objects/Classes

CS 107 Lecture 18: GCC and Make

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Something we should know

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Flow Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4

Transcription:

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7 Ziad Matni Dept. of Computer Science, UCSB

Programming in Multiple Files The Magic of Makefiles! Lecture Outline Design and Debug Tips Designing and Debugging Loops The Mighty TRACE Designing and Debugging Functions 4/25/18 Matni, CS16, Sp18 2

C++ Programming in Multiple Files Novice C++ Programming: All in one.cpp source code file All the function definitions, plus the main( ) program Actual C++ Programming separates parts There are usually one or more header files with file names ending in.h that typically contain function prototypes There are one or more files that contain function definitions, some with main( ) functions, and others that don't contain a main( ) function 4/25/18 Matni, CS16, Sp18 3

Why? Reusability Some parts of the program are generic enough that we can use them over again Reuse is not necessarily just in one program! Modularization Create stand-alone pieces of code Can contain sets of functions or sets of classes (or both) A library is a module that is in an already-compiled form (i.e. object code) Independent work flows If we have multiple people working on a project, it is a good idea to break it into pieces so that everyone can work on their files Faster re-compilations & debug When you make a change, you only have to re-compile the part(s) that have changed Easier to debug a portion than the entire program! 4

#include <etc > #include <etc > float linearscale(...); float quadraticscale(...); float bellcurve(...); float linearscale(...){... } float quadraticscale(...) {... } float bellcurve(...) {... } int main() {... } // File: MyFunctions.h #include <etc > float linearscale(...); float quadraticscale(...); float bellcurve(...); // File: MyFunctions.cpp #include MyFunctions.h float linearscale(...){... } float quadraticscale(...) {... } float bellcurve(...) {... } // File: main.cpp #include MyFunctions.cpp int main() {... } 5

Compiling Everything g++ -c MyFunctions.cpp o Myfunctions.o (creates MyFunctions.o) g++ -c main.cpp o main.o (creates main.o) The c option creates object code this is machine language code, but it s not the entire program yet The target object file here is always generated as a.o type g++ -o ProgX main.o MyFunctions.o (creates ProgX) The o option creates object code in this case, it s object code created from other object code. The result is the entire program in executable form. The object file here is always generated with the name specified after the o option. 4/25/18 Matni, CS16, Sp18 6

What Do You End Up With? MyFunctions.h Header file w/ function prototypes MyFunctions.cpp C++ file w/ function definitions MyFunctions.o Object file of MyFunctions.cpp main.cpp main.o ProgX C++ file w/ main function Object file of main.cpp Final executable file and this is a simple example!! Wouldn t it be nice to have code that generates/controls this? 4/25/18 Matni, CS16, Sp18 7

There Are Several Ways To Do This Piece-wise Approach See example1 and example2 in the demo code for this lecture (demo_lecture07) example1: similar to the one we just went through example2: by re-arranging headers, we can make one compile command (simpler, but also more limiting) 4/25/18 Matni, CS16, Sp18 8

Make Make is a build automation tool Automatically builds executable programs and libraries from source code The instructions for make is in a file called Makefile. Makefile is code written in OS-friendly code Linux OS, to be precise 4/25/18 Matni, CS16, Sp18 9

Makefile The file must be called makefile (or Makefile ) Put all the instructions you re going to use in there There is a syntax to follow for makefiles Just type make at the prompt, instead of all the g++ commands Makefiles can easily be used to do other OS-related stuff Like clean up your area, for example 4/25/18 Matni, CS16, Sp18 10

all: Exercise1 Exercise2 Syntax of a Make # This one forces the compile to use version 11 rules # Also shows me all warnings (not just errors) Exercise1: ex1.cpp g++ ex1.cpp o ex1 -std=c++11 Wall # This next one s a doozy Exercise2: ex2.cpp g++ ex2.cpp o ex2 clean: rm *.o ex2 ex1 4/25/18 Matni, CS16, Sp18 11

Note: These are TAB characters used for the indents! Don t use spaces!! Syntax of a Make Target all programs in this project all: Exercise1 Exercise2 Dependencies (macros) that are declared below # This one forces the compile to use version 11 rules # Also shows me all warnings (not just errors) Exercise1: ex1.cpp g++ ex1.cpp o ex1 -std=c++11 Wall Dependency files # This next one s a doozy Exercise2: ex2.cpp g++ ex2.cpp o ex2 clean: rm *.o ex2 ex1 # is for commenting Doesn t have to be compiling instructions only! 4/25/18 Matni, CS16, Sp18 12

What s in Your Directory? Before you run your make (and compile), your directory should have at least these files (per the example from the previous slide) ex1.cpp ex2.cpp makefile 4/25/18 Matni, CS16, Sp18 13

Using make in the Linux OS Environment Now that you have a makefile, you can execute a compiling process simply by issuing: $ make ß This is will create ALL the output executables Or you can execute make for one dependency (i.e. program) in particular, like this: $ make Exercise1 ß This is will create the output executable for Exercise1 (i.e. the file ex1 in our example) In our example, we even provided a way to clean up after we re done by deleting all the executables that we created (in case we wanted to run the compiling again, let s say) $ make clean ß This will delete all the executables that we created (like ex1 and ex2) 4/25/18 Matni, CS16, Sp18 14

4/25/18 Matni, CS16, Sp18 15

Debugging Loops Common errors involving loops include: Off-by-one errors in which the loop executes one too many or one too few times Infinite loops usually result from a mistake in the Boolean expression that controls the loop 4/25/18 Matni, CS16, Sp18 16

Fixing Off-By-One Errors Check your comparison: should it be < or <=? Saw a few mistakes like this on the exam L Check that the var. initialization uses the correct value 4/25/18 Matni, CS16, Sp18 17

Fixing Infinite Loops Common mistake: check the direction of inequalities: should I use < or >? Lean towards using < or > in your loop conditions Avoid equality (==) or inequality (!=) 4/25/18 Matni, CS16, Sp18 18

More Loop Debugging Tips: Tracing Be sure that the mistake is really in the loop Trace the variable to observe how it changes Tracing a variable is watching its value change during execution. Best way to do this is to insert cout statements and have the program show you the variable at every iteration of the loop. 4/25/18 Matni, CS16, Sp18 19

Debugging Example The following code is supposed to conclude with the variable product equal to the product of the numbers 2 through 5 i.e. 2 x 3 x 4 x 5, which, of course, is 120. What could go wrong?! J Where might you put a trace? int next = 2, product = 1; while (next < 5) { next++; product = product * next; } DEMO! Using variable tracing 4/25/18 Matni, CS16, Sp18 20

Loop Testing Guidelines Every time a program is changed, it should be re-tested Changing one part may require a change to another Every loop should at least be tested using input to cause: Zero iterations of the loop body One iteration of the loop body One less than the maximum number of iterations The maximum number of iterations If all of these are ok, you likely have a very robust loop 4/25/18 Matni, CS16, Sp18 21

Starting Over Sometimes it is more efficient to throw out a buggy program and start over! The new program will be easier to read The new program is less likely to be as buggy You may develop a working program faster than if you work to repair the bad code The lessons learned in the buggy code will help you design a better program faster 4/25/18 Matni, CS16, Sp18 22

Testing and Debugging Functions Each function should be tested as a separate unit Test functions by themselves: it make finding mistakes easier! Driver or Test Programs can help Yes: create another program to test your original program Once a function is tested, it can be used in the driver program to test other functions 4/25/18 Matni, CS16, Sp18 23

Example of a Driver Test Program 4/25/18 Matni, CS16, Sp18 24

Stubs When a function being tested calls other functions that are not yet tested, use a stub A stub is a simplified version of a function A placeholder for the real thing i.e. they re fake functions Stubs should be so simple that you have full confidence they will perform correctly 4/25/18 Matni, CS16, Sp18 25

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> #include <cmath> use namespace std; double WeirdCalc(double x, double y); int main( ) { double n, m, w; cout << Enter the 2 values for weird calculation: ; cin >> n >> m; w = WeirdCalc(n, m) / (37 pow(n/m, m/n) ); cout << The answer is: << w << endl; return 0; } Stub Example double WeirdCalc(double x, double y) { return ( (sqrt(pow(3*x, y%(max(x,y))) sqrt(5*y/(x-6)) + 0.5*pow((x+y), -0.3); } 4/25/18 Matni, CS16, Sp18 26

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> #include <cmath> use namespace std; double WeirdCalc(double x, double y); int main( ) { double n, m, w; cout << Enter the 2 values for weird calculation: ; cin >> n >> m; w = WeirdCalc(n, m) / (37 pow(n/m, m/n) ); cout << The answer is: << w << endl; return 0; } Stub Example double WeirdCalc(double x, double y) // Make WeirdCalc a stub just for testing!! { //return ( (sqrt(pow(3*x, y%(max(x,y))) sqrt(5*y/(x-6)) + 0.5*pow((x+y), -0.3); return ( 7 ); } 4/25/18 Matni, CS16, Sp18 27

Debugging Your Code: The Rules Keep an open mind Don t assume the bug is in a particular location Don t randomly change code without understanding what you are doing until the program works This strategy may work for the first few small programs you write but it is doomed to failure for any programs of moderate complexity Show the program to someone else 4/25/18 Matni, CS16, Sp18 28

General Debugging Techniques Check for common errors, for example: Local vs. Reference Parameters = instead of == Did you use && when you meant? These are typically errors that might not get flagged by a compiler!! Localize the error Narrow down bugs by using tracing and stub techniques Once you reveal the bug and fix it, remove the extra cout statements Your textbook has great debugging examples 4/25/18 Matni, CS16, Sp18 29

Pre- and Post-Conditions Concepts of pre-condition and post-condition in functions We recommend you use these concepts when making comments Pre-condition: What must be before you call a function States what is assumed to be true when the function is called Function should not be used unless the precondition holds Post-condition: What the function will do once it is called Describes the effect of the function call Tells what will be true after the function is executed (when the precondition holds) If the function returns a value, that value is described Changes to call-by-reference parameters are described 4/25/18 Matni, CS16, Sp18 30

Why use Pre- and Post-conditions? Pre-conditions and post-conditions should be the first step in designing a function Specify what a function should do BEFORE designing it This minimizes design errors and time wasted writing code that doesn t match the task at hand 4/25/18 Matni, CS16, Sp18 31

Example void write_sqrt(double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output { cout << sqrt(x) << endl; } 4/26/18 Matni, CS16, Sp18 32

YOUR TO-DOs q Finish Lab3 by next Monday q Prepare Lab4 for next Wednesday q Do HW7 by next Tuesday q Visit Prof s and TAs office hours if you need help! q Hug a tree! (or a loved one will do) 4/25/18 Matni, CS16, Sp18 33

4/25/18 Matni, CS16, Sp18 34