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