Compilation and Debugging

Slides:



Advertisements
Similar presentations
Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Advertisements

A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
Understanding Makefiles COMP 2400, Fall 2008 Prof. Chris GauthierDickey.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
Guide To UNIX Using Linux Third Edition
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Testing and Debugging Hakam Alomari
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
The Unix Environment and Compiling. Getting Set Up Your programs will be compiled and tested on the Departmental server ‘linprog’ The linprog servers.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
General Programming Introduction to Computing Science and Programming I.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Creating your first C++ program
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
UNIT 13 Separate Compilation.
Makefile M.A Doman. Compiling multiple objects Card.cpp -> Card.o Deck.cpp -> Deck.o main.cpp -> main.o main.o Deck.o Card.o -> Dealer.exe.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Object Oriented Programming COP3330 / CGS5409.  Assignment Submission Overview  Compiling with g++  Using Makefiles  Misc. Review.
COP 3530 Spring 12 Discussion Session 1. Agenda 1.Introduction 2.Remote programming 3.Separate code 4.Compile -- g++,makefile 5.Debug -- gdb 6.Questions?
Make Make is a system utility that automatically compiles your programs for you Make looks for a file named Makefile (or makefile) in the current directory.
Multiple File Compilation and linking By Bhumik Sapara.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
1 Introduction to Object Oriented Programming Chapter 10.
Object Oriented Programming COP3330 / CGS5409.  Compiling with g++  Using Makefiles  Debugging.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects can become huge My research consists of 1600.
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
Object Oriented Programming COP3330 / CGS5409.  Assignment Submission Overview  Compiling with g++  Using Makefiles  Misc. Review.
Recursion ● Recursion is a computer programming technique that allows programmers to divide problems into smaller problems of the same type. ● You can.
Review Why do we use protection levels? Why do we use constructors?
Regarding assignment 1 Style standards Program organization
What Is? function predefined, programmer-defined
Jie(Jay) Wang Week1 Sept. 30
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Completing the Problem-Solving Process
Andy Wang Object Oriented Programming in C++ COP 3330
Compilation and Debugging
Separate Compilation and Namespaces
Makefiles Caryl Rahn.
CS1010 Programming Methodology
Introduction to C Topics Compilation Using the gcc Compiler
CS1010 Programming Methodology
Chapter 2 – Getting Started
Separate Compilation and Namespaces
Separate Compilation.
Makefiles and the make utility
Andy Wang Object Oriented Programming in C++ COP 3330
Separating Definition & Implementation
Namespaces How Shall I Name Thee?.
A programming language
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
CSCE-221 Makefile Introduction
Separate Compilation.
Andy Wang Object Oriented Programming in C++ COP 3330
Computer Terms Review from what language did C++ originate?
What Is? function predefined, programmer-defined
Makefiles and the make utility
CSC 253 Lecture 15.
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Chapter 1 c++ structure C++ Input / Output
SPL – PS1 Introduction to C++.
g++ features, better makefiles
Presentation transcript:

Compilation and Debugging Andy Wang Object Oriented Programming in C++ COP 3330

Multiple File Projects For small programs, everything can fit in one file For large programs, need to use separate files to organize code A class typically consists of a pair of files A header file (.h) that contains the class declaration An implementation file (.cpp) that defines and implements the class member functions

File Names Do not need to match the class name For a class, you can name the header file <className>.h and the implementation file <className>.cpp circle.h // header file for a class called Circle circle.cpp The main program is usually written in a separate file

Compilation Two major stages Compile stage Checks syntax and types Matches member function calls with their declarations in the header files No need to know the definitions of member functions yet Translates source code (.cpp files) into temporary object code (machine code .o files), not an executable program Not to be confused with the “object” used in object oriented programming

Compilation Two major stages Linking stage Links object code into an executable program May involve one or more object code files Makes sure that all member functions are implemented, with no duplications The end of result of linking is usually an executable program

To Create a Multi-file Project One possible organization Essentially combines two files into one NOT a good idea // frac declaration // frac definition #include “frac.cpp” // main function frac.cpp main.cpp

Separate Compiling and Linking A better organization Benefits Change to a file requires recompiling only that file Along with the linking // frac declaration frac.h #include “frac.h” // frac definition #include “frac.h” // main function frac.cpp main.cpp

Rule of Thumb Only #include the header files Not the .cpp files!

Example g++ Commands Translates frac.cpp into frac.o g++ -c frac.cpp Translates main.cpp into main.o g++ -c main.cpp Links .o files into an executable file called frac g++ frac.o main.o –o frac

Makefiles The make utility eases the process of compilation Puts various compilation commands into either a Makefile or makefile To compile, just type make

The Makefile Format Consists of many sections The basic format <target_name>: <dependency list> <commands> <target_name> depends on the items in the <dependency list> To create <target_name>, run <commands> If anything is changed in the <dependency list>, make will automatically rerun <commands> <commands> refers to Unix commands Each command must be preceded by a single tab

Example Makefile # This is a comment line frac: main.o frac.o g++ main.o frac.o –o frac main.o: main.cpp frac.h g++ -c main.cpp frac.o: frac.cpp frac.h g++ -c frac.cpp clean: rm *.o frac

To Compile Type make make clean make attempts to build the first target name in the Makefile In this case, make is the same as make frac make clean Defined in the last section of Makefile Will remove the .o files and the executable from the directory

Types of Errors Compilation errors Linker errors Runtime errors Syntax errors Undeclared variables and functions Improper function calls Linker errors Undefined functions Functions defined multiple times Runtime errors Crashes Erroneous results

Types of Errors Compilation and linker errors result in failed compilation Runtime errors occur while the program is running After successful compilation

Debugging Compilation errors Reported by the compiler Tips Usually with a file name and a line number Tips Always start with the first reported error If the list is long, compile and debug one file at a time An error may be caused by the line prior to the line number reported E.g., missing ‘;’ Compile and debug each function as you go Don’t wait until after the entire file is written to compile it

Debugging Linking stage errors Usually do not report line numbers Tips Undefined functions may result from forgetting to include a header file Functions defined multiple times may mean name collisions

Example Compilation Errors http://www.cs.fsu.edu/~myers/cop3330/examples/debug/ Missing ‘”’ or ‘;’ Mismatching declarations and definitions Missing class scope information Missing declarations Broken comments Wrong return type Missing class variable when invoking member functions

Example Compilation Errors Wrong syntax ‘,’ instead of ‘;’ Passing the wrong number of parameters Duplicate definitions

Runtime Errors Must be tested while running a fully-compiled program Tips For a crash Add printout statements to find the location prior to crash #include <iostream> using namespace std; cout << __FILE__ << “:“ << __LINE__ << endl; For logic errors Print out internal variables to locate computation problems Remember to remove print statements used for debugging in the submitted version