20
CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Embed Size (px)

Citation preview

Page 1: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

CPS120: Introduction to Computer Science

Compiling Your Programs Using

Visual C++

Page 2: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Compilers

• Engines that work 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

Page 3: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Compilers Available

• Products range from freeware compilers to work environments designed for commercial application development

–Borland C++ compiler

–CodeWarrior

–IBM Visual Age C++

–Microsoft Visual C++

–GNU freeware

–DJGPP freeware (  www.delorie.com/djgpp/ )

Page 4: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Compilation Process

1. Get the set of instructions from you2. 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

Page 5: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Compiler Files

• Source code

• Linked/Library file

• Object file

• Executable file

Page 6: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Source Code

• The set of instruction that you will develop on your own for processing by the compiler

Page 7: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Executable Files

• A machine-language executable file created when the compilation process is complete– All that is needed to run the program– Not human-readable– Has the extension .EXE– Stored in binary form

• Copies may be distributed (portable)

– Known as an application

Page 8: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Compiling 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

Page 9: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Creating Source Code Files

Actually Compiling a Program

Page 10: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Creating Source Code

• Programmers spend most of their time with source code files– Need to be comfortable with an editor that

creates ASCII text files• 7-bit representation of all keyboard character

– Don’t use a word processor– If an editor in an environment is available, use

that, it provides more sophisticated tools

Page 11: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Using the Editor

• Save often because there is no autosave in Visual C++

• The editor will do matching of delimiters and intelligent indentation

Page 12: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Columns and White Space• Modern 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

Page 13: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Color Coding in Visual C++ Editor

• Comments are green and are ignored by the compiler

• All ANSI keywords are coded in blue

• Other code is in plain black– Compiler keywords like cin and cout are also

shown in black

Page 14: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Setting Up a Visual C++ Workspace1. 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

Page 15: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Setting Up a Visual C++ Workspace7. Create the directory with START / EXPLORE

8. Double-click the drive letter

9. Choose FILE, NEW FOLDER

10. Left click on FOLDER

11. Change new folder to cppFUN

12. Close Explorer with the X

13. Back in Visual C++, type myFirstCpp in the file box

14. Click OK and get back to the main edit screen

Page 16: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Setting Up a Visual C++ Workspace

15. Enter the source code

16. After entering the program, FILE then SAVE

Page 17: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Why Create New Subdirectories?

• You should always use subdirectories to store your files. Visual C++ creates quire 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.

• Under no circumstances should you use the root (C:\) directory except on a floppy disk (A:\).

Page 18: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Running 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

Page 19: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Disk Space Issues

• Text files are insignificant in terms of space

• However, six other files are created every time something is compiled; some get around 95% full– Highlighting the disk in are 3 or 4 times larger

than the source– You can delete anything but the file with

the .cpp suffix; everything else can be recreated

Page 20: CPS120: Introduction to Computer Science Compiling Your Programs Using Visual C++

Recompiling1. Open the file by going the file you stored it in and

double clicking on it

2. Scan the source code for obvious errors before attempting to compile

3. Attempt to compile using the COMPILE button or choosing the first option on the BUILD menu

4. Answer Yes to use the default project workspace– The lower window will be active and the compile will occur

5. Note the number of errors and warnings– These need to be dealt with before executable code is created