Chapter 2. Editing And Compiling

Similar documents
Notes By: Shailesh Bdr. Pandey, TA, Computer Engineering Department, Nepal Engineering College

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Eclipse Environment Setup

Programming Principles 1 (CSC131) & 2 (CSC132) Software usage guide

Running Java Programs

SPSS Tutorial - How to Perform an Offline License Activation on a Windows Computer

Using the Dev C++ Compiler to Create a Program

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Compiling and Running an Application from the Command Line

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Lab 1 Introduction to UNIX and C

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

Unit 13. Linux Operating System Debugging Programs

CSCI 161: Introduction to Programming I Lab 1b: Hello, World (Eclipse, Java)

CPSC 150 Laboratory Manual. Lab 1 Introduction to Program Creation

Lab: Supplying Inputs to Programs

Department of Computer Science. Software Usage Guide. CSC132 Programming Principles 2. By Andreas Grondoudis

Writing and Running Programs

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

My Favorite bash Tips and Tricks

Software Installation for CS121

ADOBE DREAMWEAVER CS4 BASICS

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Getting started with UNIX/Linux for G51PRG and G51CSA

Unit 10. Linux Operating System

A Tutorial on using Code::Blocks with Catalina 3.0.3

The Command Shell. Fundamentals of Computer Science

Physics 306 Computing Lab 1: Hello, World!

Laboratory 1 Semester 1 11/12

Starting. Read: Chapter 1, Appendix B from textbook.

RAGE WebDesign Quick Start 1 of 18. Welcome To RAGE WebDesign

C++ for Python Programmers

Tutorial 1: Unix Basics

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Running Wordstar 6 on Windows 7 Using vdos

This chapter is intended to take you through the basic steps of using the Visual Basic

Adobe Dreamweaver CS5 Tutorial

4. Some computers may also be customised so that a program such as Word can be started using a keyboard command.

In this lab, you will build and execute a simple message flow. A message flow is like a program but is developed using a visual paradigm.

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

Introduction. SSH Secure Shell Client 1

9/2/2016. Storage Media. Storage Media

Lab 3a Using the vi editor

eftp Application User Guide

Using the Zoo Workstations

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Burning CDs in Windows XP

Windows Key e: Press and hold down the Windows (Start) Key and tap the letter e one time. Release these keys and Windows File Explorer will open.

Parts of this tutorial has been adapted from M. Stonebank s UNIX Tutorial for Beginners (

Tutorial 2: Compiling and Running C++ Source Code

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Getting Started with Python and the PyCharm IDE

Once you have installed MobaXterm, open MobaXterm. Go to Sessions -> New Session, and click on the SSH icon.

The main differences with other open source reporting solutions such as JasperReports or mondrian are:

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Semester 2, 2018: Lab 1

Problem Set 1: Unix Commands 1

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3. Installing Notepad++

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Do this by creating on the m: drive (Accessed via start menu link Computer [The m: drive has your login id as name]) the subdirectory CI101.

Windows Script Host Fundamentals

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CSE115 Lab exercises for week 1 of recitations Spring 2011

Temple University Computer Science Programming Under the Linux Operating System January 2017

HELPLINE. Dilwyn Jones

HTML&CSS. design and build websites

CS Prof J.P.Morrison

An Introduction to DOS

ITST Searching, Extracting & Archiving Data

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Lab # 2. For today s lab:

Linux File System and Basic Commands

1) Log on to the computer using your PU net ID and password.

Chapter 2. Basics of Program Writing

Math Modeling in Java: An S-I Compartment Model

Additional laboratory

INSTALLING AN SSH / X-WINDOW ENVIRONMENT ON A WINDOWS PC. Nicholas Fitzkee Mississippi State University Updated May 19, 2017

Chapter 1 - What s in a program?

Lab 1: Accessing the Linux Operating System Spring 2009

XP: Backup Your Important Files for Safety

Instructions. First, download the file

Understanding structure Learning about markup Tags and elements. Structure COPYRIGHTED MATERIAL

WA2031 WebSphere Application Server 8.0 Administration on Windows. Student Labs. Web Age Solutions Inc. Copyright 2012 Web Age Solutions Inc.

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

Assignment 1. Application Development

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

Configuring Ubuntu to Code for the OmniFlash or OmniEP

Department of Computer Science University of Pretoria. Introduction to Computer Science COS 151

Slide 1 Java Programming 1 Lecture 2D Java Mechanics Duration: 00:01:06 Advance mode: Auto

vi Primer Adapted from:

Applied ICT Skills MS Windows

EECS 1710 SETTING UP A VIRTUAL MACHINE (for EECS labs)

Introduction to Python Code Quality

My First iphone App. 1. Tutorial Overview

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

1 Getting used to Python

Transcription:

Chapter 2. Editing And Compiling Now that the main concepts of programming have been explained, it's time to actually do some programming. In order for you to "edit" and "compile" a program, you'll need a program to type (remember the "understanding" step?), an editor, and a compiler program. Some of the instructions for editing and compiling are system-dependent. The presentation in this chapter is specific to Microsoft Windows. But where appropriate to do so, alternate instructions for Apple MacOSx and UNIX/Linux are included. Here's a checklist of the things you will need to consider: Checklist Choose an editor. Install, if necessary. Configure for your use. Choose a compiler. Install, if necessary. Configure for your use. Choose a folder on your system for storing your files. Configure Windows (if you are using Microsoft) Once this checklist is completed, you will be ready to edit and compile your first C++ program! 2.1 Choosing An Editor The first thing to do is to choose an editor. Our choice is Windows Notepad, which can be found under Start All Programs Accessories Notepad. No installation is necessary for Notepad. 2.1.1 Configuring XP Notepad Two settings need to be made in Notepad, so that you can see line numbers. The first is to turn off "word wrap" -- to do so, use the Format menu. Make sure that there is no checkmark next to the "Word Wrap" option. If there is, click it to make it go away. Then turn on the status bar. To do so, use the View menu. If there is a checkmark beside "Status Bar", leave it. Otherwise, click it. The Format and View menus should look like this:

On your own computer, these settings will persist from session to session. But in the computer lab, you probably will have to configure these each time you sit at a computer work station. 2.1.2 Installing JNotePad The JNotePad editor was written by the author of this textbook. It is similar to Notepad, except that it works on almost any operating system, including Windows, Apple, and versions of Linux with a graphical user interface. It also contains menu-accessible "code block" templates that can be used to insert the C++ and/or Java code necessary to perform certain common programming tasks, such as opening a file. The JNotePad window looks like this: The installation of JNotePad is very easy -- it just involves copying its startup icon from the internet (http://www.rdb3.com/jnotepad/). to your desktop. But in order for Windows users to use the icon, they first need to download and install the Java Runtime Environment (JRE) using this URL: http://www.java.com/en/download/manual.jsp. 2.2 Choosing A Compiler There is quite a variety of C++ compilers available. Your system may already have a C++ compiler installed on it. For example, Linux and UNIX computers often have GNU installed. To find out if GNU is installed, go to a "command prompt" and enter the command g++. If the command is recognized, as indicated by the message "no input files", then you have the GNU C++ compiler. But Windows computers usually do not have GNU, and installation of a compiler is required. Microsoft Visual C++ (which is a module of Visual Studio) is a very popular compiler, and is available in several versions. Microsoft Visual C++ Express can be downloaded and installed for free! The most recent release is Visual C++ 2008. The instructions and examples presented in this book apply to versions of the Microsoft Visual C++ compiler after Version 6.0, which was released at about the time that Windows 95 came out. Go to http://www.microsoft.com/express/vc to get started. Alternatively you can use just about any other C++ compiler, as long as it was released after Windows 95. We will use Visual C++'s "command line" compiler for the examples in this text. 2.2.1 Confirming Your Visual C++ Installation After you have installed a Visual C++ compiler, you should verify that it is properly installed so that you will be able to use it. To do so, you need to get a "command prompt" window and enter a compiler command, as explained

below. You can also use this procedure to confirm on any Windows computer that it has Visual C++ installed on it. Do Start All Programs Visual C++ 9.0 Express Edition Visual Studio Tools Visual Studio 2008 Command Prompt, like this: Note that you may see references to both 8.0 and 9.0, and to both "Visual Studio" and "Visual C++". Don't worry about these -- the naming and numbering by Microsoft for these versions is not very consistent! Also note that if the computer has other versions of Visual C++ (later than version 6.0), the menu item names will vary slightly from the ones explained here. You will see a "command prompt" window, similar to this (but probably with a folder specification other than "C:\"): Enter the command cl -- that's see-el, not see-one. It does not matter what the folder specification is -- it should work with "C:\" or anything else. You should see something silimar to the following: If you see something like the above, with reference to the compiler version number, then it works! But if you see something like the following instead, then it is not working, and you will need to verify that you did the installation and confirmation steps correctly. One possible explanation for seeing the above failed result is opening the command prompt window directly, through

the Start All Programs Accessories menu or by running the command "cmd". Unless you follow additional configuration steps which are not included in this book, then you need to get to the command window as explained above. 2.3 Choosing A Folder For Storing Files It's best to decide upon this before writing your first program. You can store your files on a hard drive or on removable media. For speed of access, use a hard drive folder -- floppy drives are annoyingly slow! To navigate to the root folder in the command line mode of Windows, use the cd\ command ("change directory"). To navigate to any other folder, use a command like cd rdb to get from the root to a folder named "rdb", for example. (Of course, you have to create the folder with Windows Explorer first, or use a typed command like md rdb -- "make directory" -- to create it.) To log into another drive, enter a command with the drive letter followed by a colon -- a: logs you into the floppy drive. The folder whose name appears in the prompt is the working folder. In the above screen shots, the working folders are C:\rdb and A:\ (that is, the root of the A: drive), respectively. 2.3.1 How To Backup Your Files The rule is: "only backup the files that you do not want to lose." After you finish editing your code files, and you have saved them on a hard drive or removable media, imagine for a moment what would happen if the hard drive fails tonight or if you lose your diskette. It's a good idea to backup your files by putting a copy of them on the Internet. There are several options, including email and FTP. You may have an email or Internet service account, such as comcast.net, which provides free "personal web space" -- you can use the interface that they provide for exchanging files. You can send files to yourself in email attachments as another way to backup your files. Ideally, you start your work by retrieving copies of your files from their Internet location, and save them to your local hard drive in a folder of your choosing. Then after your coding work is done, put new copies back on the Internet. This not only frees you from worrying about losing your work, but it also enables you to do your work on a variety of

computers (with Internet access). (And if your "backup" is your only permanent copy of your file, then you need a second form of backup -- either another Internet location or removable media.) 2.4 Configuring Windows For Programming Actually, you can use Windows for programming without any configuration changes. But there is one change that makes things easier for identifying files. The problem is that Windows hides filename extensions by default. That is, the compiled program file hello.exe may appear as hello in a file listing. To change this behavior, start Windows Explorer and use the menu command "Tools Folder Options". Click the "View" tab, and remove the checkmark from "Hide extensions for known file types". On your own computer, this setting will persist from session to session. But in the computer lab, you will have to configure this each time. 2.5 Editing Okay -- we are ready to program! Let's start by writing the world's smallest C++ program, and compiling that. It actually doesn't do anything, but if you are able to get this far, then you can at least establish that you can use an editor and that you have a compiler. With these details out of the way, we can focus on learning to program! Here's our first program: The World's Smallest C++ Program int main() { return 0; }

Start up your text editor. The examples in this text will show Notepad, but you can just as easily use any other editor and system. So if you are using Apple, you can use TextEdit. And if you are using Linux or UNIX, use vi or any other text editor with which you are familiar. Note that there are some lines containing "curly braces". Also note that one line is indented from the left margin, and others start at the left margin. Use 2 spaces to indent. It is possible to use a tab instead of 2 spaces, and many programmers do just that. But tab sizes are variable among editors, so that if you look at your code in one editor, it may not look the same as it does in another editor. Worse, if you use spaces sometimes and tabs sometimes, what looks fine in one editor may look jumbled in another. Type the code as shown above. Remember that case matters, so type lowercase letters as lowercase letters. Also, that's a "zero", not an "oh" following "return". 2.5.1 How To Type Code The tendency is to type code "linearly" that is, from the top line to the bottom. But that is not the best way to type code. Matching parentheses and curly braces appear throughout code, and the toughest part is to keep track of these and make sure that each open parenthesis has a matching closing parenthesis. The easiest way to do that is to type opening and closing symbols together, and then separate them. In the example above, you should type the opening curly brace, then on the next line type the closing curly brace. Then make a blank line between the two and type "return 0;". 2.5.2 Saving A File Save the file as hello.cpp, into the folder you chose for storing your programming files (c:\rdb in the examples in this text). If you use XP Notepad, be sure to enclose the filename in quotes, or else.txt may be appended to the filename! The CPP file is called the "source file", and it contains "source code". Note that as you type the program, there is an indication on the editor about the line number which is currently being edited. In XP Notepad this is located in the lower right with the abbreviations "Ln" for line number and "Col" for column number. (If this does not appear, then revisit section 2.1.1 above.) This is not so important for now, but knowing the line number will be important when we get to the compiling step, in case the compiler detects and reports typing errors.

2.6 Compiling And Running Now that the program has been saved to the drive, you are ready to compile it into a working program file! We will use Microsoft Visual C++ in command line mode. First, go to a command prompt as explained above, and navigate to the drive and folder containing your edited file. Run the compiler by typing this command: using Visual C++, cl hello.cpp -EHs using GNU, g++ hello.cpp -o hello If there are any errors, it should be evident from the output of the compiler. Line numbers should also appear in the output of the compiler, guiding you to the problem. If successful, the Visual C++ compiler will have created hello.obj and hello.exe in the same folder. The GNU compiler will have created hello.o and hello. The first file is called an "object file", and it can be discarded. The second is called the "Executable". It is the actual program file. You can distribute this file to people using the same operating system, and they will be able to run it. To run, type this command: hello Don't be disappointed if nothing happens -- remember that this is the world's smallest C++ program, and it does nothing. It collects no input and it produces no output. But at least it does not produce an error! In this very simple programming example, you completed the editing step before you started the compiling step. In all other cases you will mix these steps -- that is, you will write part of the code, save, and compile, and then write more code and repeat this process. Just remember to save and compile often. Also, you should backup your work at the end of every editing session. Backup only the files that you type -- there is no need to backup the files generated by the compiler. 2.6.1 Recompiling When you use the command line to compile your programs, it will seem as if there is a lot of typing to do -- repetitive typing. But you do not have to retype the compile command over and over again. Most systems let you use the UP and DOWN ARROWs of the keyboard to recall a recently-typed command. On Windows systems, you can also use the F7 key to get a menu of recently-typed commands, although the UP ARROW is the easiest way to recall the last-typed command. We will use Microsoft Visual C++ in command line mode.