Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compilation and Debugging

Similar presentations


Presentation on theme: "Compilation and Debugging"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

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

9 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

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

11 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

12 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

13 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

14 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

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

16 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

17 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

18 Example Compilation Errors
Missing ‘”’ or ‘;’ Mismatching declarations and definitions Missing class scope information Missing declarations Broken comments Wrong return type Missing class variable when invoking member functions

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

20 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


Download ppt "Compilation and Debugging"

Similar presentations


Ads by Google