Publications: 117 | Followers: 0

Multiple File Compilation - Computer Science, FSU

Publish on Category: Birds 0

Compilationand Debugging
Andy WangObject OrientedProgrammingin C++COP 3330
Multiple File Projects
For small programs, everything can fit in one fileFor large programs, need to use separate files to organize codeA class typically consists of a pair of filesA header file (.h) that contains the class declarationAn implementation file (.cpp) that defines and implements the class member functions
File Names
Do not need to match the class nameFor a class, you can name the header file <className>.h and the implementation file <className>.cppcircle.h// header file for a class called Circlecircle.cppThe main program is usually written in a separate file
Two major stagesCompile stageChecks syntax and typesMatches member function calls with their declarations in the header filesNo need to know the definitions of member functions yetTranslates source code (.cppfiles) into temporary object code (machine code .o files), not an executable programNot to be confused with the “object” used in object oriented programming
Two major stagesLinking stageLinks object code into an executable programMay involve one or more object code filesMakes sure that all member functions are implemented, with no duplicationsThe end of result of linking is usually an executable program
To Create a Multi-file Project
One possible organizationEssentially combines two files into oneNOTa good idea
#include “frac.cpp”// main function
Separate Compiling and Linking
A better organizationBenefitsChange to a file requires recompiling only that fileAlong with the linking
#include “frac.h”//fracdefinition
#include “frac.h”// main function
Rule of Thumb
Only #include the header filesNot the .cppfiles!
Example g++ Commands
Translates frac.cpp intofrac.og++ -cfrac.cppTranslates main.cpp intomain.og++ -cmain.cppLinks .o files into an executable file calledfracg++frac.omain.o–ofrac
Themakeutility eases the process of compilationPuts various compilation commands into either aMakefileormakefileTo compile, just typemake
Consists of many sectionsThe 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>, makewill automatically rerun<commands><commands> refers toUnix commandsEach command must be preceded by a single tab
# This is a comment linefrac:main.ofrac.og++main.ofrac.o–ofracmain.o: main.cppfrac.hg++ -c main.cppfrac.o: frac.cppfrac.hg++ -c frac.cppclean:rm*.ofrac
To Compile
Typemakemake attempts to build the first target name in theMakefileIn this case,makeis the same asmakefracmake cleanDefined in the last section ofMakefileWill remove the .o files and the executable from the directory
Types of Errors
Compilation errorsSyntax errorsUndeclared variables and functionsImproper function callsLinker errorsUndefined functionsFunctions defined multiple timesRuntime errorsCrashesErroneous results
Types of Errors
Compilation and linker errors result in failed compilationRuntime errors occur while the program is runningAfter successful compilation
Compilation errorsReported by the compilerUsually with a file name and a line numberTipsAlways start with the first reported errorIf the list is long, compile and debug one file at a timeAn error may be caused by the line prior to the line number reportedE.g., missing ‘;’Compile and debug each function as you goDon’t wait until after the entire file is written to compile it
Linking stage errorsUsually do not report line numbersTipsUndefined functions may result from forgetting to include a header fileFunctions defined multiple times may mean name collisions
Example Compilation Errors
Example ‘”’ or ‘;’Mismatching declarations and definitionsMissing class scope informationMissing declarationsBroken commentsWrong return typeMissing class variable when invoking member functions
Example Compilation Errors
Wrong syntax‘,’ instead of ‘;’Passing the wrong number of parametersDuplicate definitions
Runtime Errors
Must be tested while running a fully-compiled programTipsFor a crashAdd printout statements to find the location prior to crash#include <iostream>using namespacestd;cout<< __FILE__ << “:“ << __PRETTY_FUNCTION__ << “:”<<__LINE__ <<endl;For logic errorsPrint out internal variables to locate computation problemsRemember to remove print statements used for debugging in the submitted version





Make amazing presentation for free
Multiple File Compilation - Computer Science, FSU