cps120: introduction to computer science compiling your first program
TRANSCRIPT
CPS120: Introduction to Computer Science
Compiling Your First Program
CompilersCompilers
An engine that works on your behalf to process instructions and allow you to deal with various basic rules of the language
The compiler’s job is to make sure you follow the rules, to require that you provide enough information that the compiler can translate you instructions into languages the components can understand
Compilers AvailableCompilers Available
Products range from freeware compilers to work environments designed for commercial application development
Borland C++ compilerCodeWarriorIBM Visual Age C++Microsoft Visual C++GNU freewareDJGPP freeware ( www.delorie.com/djgpp/ )
Compilation ProcessCompilation Process
1. Get the set of instructions from you (source file)2. Review the instructions to see if they violate the rules
(syntax) of the language3. If all the rules are obeyed, create a working file in the
language of the computer (machine language)4. Attach to the working file full instructions for any
shortcuts you may have used (linkage)5. Assemble a final file in machine language
(executable)
Source CodeSource Code
The set of instruction that you will develop on your own for processing by the compiler
Executable FilesExecutable Files
A machine-language executable file created when the compilation process is complete
All that is needed to run the programNot human-readableHas the extension .EXEStored in binary form
Copies may be distributed (portable)Known as an application
Compiling and DebuggingCompiling and Debugging
Executable code will not be created until you correct all of the syntax errors in your source code
Then the fun (with logic errors) begins
Creating Source Code Files
Actually Compiling a Program
Creating Source CodeCreating Source Code
Programmers spend most of their time with source code files
Need to be comfortable with an editor that creates text files
Don’t use a word processor
Using the Visual Studio Editor
Using the Visual Studio Editor
Save often because there is no autosave in Visual C++
The editor will do matching of delimiters and intelligent indentation
Using C++
Actually Compiling a Program
C++ Usages & Conventions
C++ Usages & Conventions
C++ is absolutely case sensitiveFor Instance: A is 97 in ASCII and a is 65Remember: in ASCII {, [, and ( are not equivalent
No keywords in ANSI standard are even partially uppercase
‘While’ is not a keyword, ‘while’ is
CommentsComments
Document what is happening, why it is happening and other issues
Commentary is ignored by the compilerC++ has inline, block and documentary comments
Inline comments are within line of codeUse the // symbols
Block comments are long comments delimited with /* and */
Scope DelimitersScope Delimiters
A symbol or pair of symbols used to define a region or area which is considered a locale
In programming, many structures need to have their scope defined because they should not affect the entire program
In C++, the symbols ‘{‘ and ‘}’ are used
LiteralsLiterals
Literals are system commands and other pieces of information that the compiler doesn’t understand, and therefore, takes your word for them
In C++, literals are enclosed in straight double quotes " " which is the shift of the apostrophe
Columns and White SpaceColumns and White SpaceModern programming languages are free form with
delimiters instead of columns to determine the end of instructions
The ; (semi-colon) is the delimiter used in C++
Use tabs, indents, and blank lines in any manner that makes code easier to understand
Many programming instructions become subordinate to other instructions due to scope and other restrictions. Formatting code to reflect this makes it easier to read
VariablesVariables
Variables or identifiers are used to hold information
Usually mixed case with the first letters small and the rest starting with a capital
e.g. theWeight
Color Coding in Visual C++ Editor
Color Coding in Visual C++ Editor
Comments are green and are ignored by the compiler
All ANSI keywords are coded in blueOther code is in plain black
Compiler keywords like cin and cout are also shown in black
Setting Up a Visual C++ Workspace
Setting Up a Visual C++ Workspace
1. Left-click START2. In the program section, select Visual Studio or Visual
C++ (depending on what is installed)3. Left click on the Visual C++ icon to load the
environment4. Create a new work area by choosing FILE/NEW5. Choose FILES tab6. Click on the C++ Source File to reach the editor
i. Add a filename and directory before continuing e.g. c:/cppFun/myFirstCpp
Setting Up a Visual C++ Workspace
Setting Up a Visual C++ Workspace
7. Create the directory with START / EXPLORE8. Double-click the drive letter9. Choose FILE, NEW FOLDER10. Left click on FOLDER11. Change new folder to cppFUN12. Close Explorer with the X13. Back in Visual C++, type myFirstCpp in the file box14. Click OK and get back to the main edit screen
Setting Up a Visual C++ Workspace
Setting Up a Visual C++ Workspace
15. Enter the source code16. After entering the program, FILE then SAVE
Why Create New Subdirectories?
Why Create New Subdirectories?
You should always use subdirectories to store your files. Visual C++ creates quite a few work files when it generates the executable file for you source code, and they will be easy to dispose of if you keep everything together in one convenient place.
Running the ProgramRunning the Program
1. Press the REBUILD ALL button. It has two arrows in a box
2. Press the RUN button. It is a red exclamation point
Disk Space IssuesDisk Space Issues Text files are insignificant in terms of space However, at least six other files are created
every time something is compiled Some of these are significantly larger
You can delete anything but the file with the .cpp suffix; everything else can be recreated
You probably also want the .exe file
An Introduction to Debugging
CPS120Introduction to Computer ScienceLecture 5
Compiling and DebuggingCompiling and Debugging
Executable code will not be created until you correct all of the syntax errors in your source code
Syntax and Logic ErrorsSyntax and Logic Errors
A syntax error is simply the violation of the rules of a language; misuse of structure and form in programming or a violation of the compiler’s rules. These errors are detected by the compilerAlso know as 'fatal compilation errors'
A logic error is a mistake that complies with the rules of the compiler that causes the program to generate incorrect output
Linker ErrorsLinker Errors
Not all syntax errors are detectable by the compilerThese errors do not become apparent until files are
put together to create an executableThese errors are not linked to a specific line of code
Look for the name of the variable and see what lines of code it occurs on using EDIT and FIND> LNIK2001: unresolved external > LNK1120: unresolved externals
DebuggingDebugging
Debugging is the process of locating and fixing or bypassing bugs (errors) in computer program code or the engineering of a hardware device.
To debug a program or hardware device is to start with a problem, isolate the source of the problem, and then fix it.
Debugging ObjectiveDebugging Objective
1. Find the line(s) containing the syntax error(s) using the compiler's chosen line and error messages as a starting point
Debugging is an ArtDebugging is an Art
Compilers often miss reporting an actual error and report on subsequent lines which are effected by error but may be completely correct
After encountering a real syntax error, compilers often generate many incorrect syntax error messages
Different compilers produce different errors and warnings for the same errors in the same program
Debugging StepsDebugging Steps
1. Proofread before compiling2. Compile3. Correct all the obvious errors
> Start at the beginning of the list of errors and warnings> A single syntax error may cause the compiler to believe numerous
other syntax errors are occurring> Look at the error lines and if you see the error, fix it. Otherwise, leave
it for later. It may vanish when you fix something else> Don’t worry if more errors appear. Some errors mask other errors
Debugging StepsDebugging Steps
4. Recompile when you have fixed what you recognize
5. Repeat 3 & 4 until no further errors are obvious6. Attempt to solve the remaining errors in a top-
down fashion7. Solve whatever errors you can without spending
long periods of time on any given error8. Recompile whenever you feel you don’t see any
further solutions
Debugging AidsDebugging Aids
1. In the Visual C++ (and other GUI-based compilers) double-clicking on an error message move the cursor to the line where the compiler detected the error This may not be the actual line where the error occurred –
don’t trust the compiler on lines2. Work from the beginning of the program, because in
most compilers, the errors are detected from the beginning to end, sequentially
3. Some errors are so severe, they stop the compiler from continuing so more errors may appear after you successfully fix one or more
A Debugging MindsetA Debugging Mindset
Assume your syntax is wrong. Look it up!Add working comments as you change things
If you are 100% sure a line is correct, then search for a syntax error in the lines ABOVE that lineStart with the immediately previous line and work
backwardNever make a change you can’t explain
Sample Debugging Comment
Sample Debugging Comment
cuot << "This is a line of code"<< endl;/*************Debug*************Error is undeclared identifier1. Checked syntax for endl2. Check syntax for screen output
-- cuot is misspelled
*/
WarningsWarnings
Actions that may represent problems but do not cause the compiler to flag an errorDon’t ignore warningsMost common warning is a ‘typecasting’ warning
Indicates that the conversion of one type of a number was moved to a number (variable) of a different type without inclusion of a typecasting operation> E.G. – Moving a float to an integer variable
Disk Space IssuesDisk Space Issues
If the floppy is full or becomes full during the compilation process, the compile will fail with an error message such as: fatal error C1033: cannot open program database
A very cryptic message like this can result
If you are not able to view all of the intermediate files created in a compile, suspect a space error