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

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

Intermediate Programming, Spring Misha Kazhdan

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

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

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

ENERGY 211 / CME 211. Evolution

CS101 Linux Shell Handout

Unit 10. Linux Operating System

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

Introduction to Linux Environment. Yun-Wen Chen

Chapter 1 Introduction to Computers and C++ Programming

Lab 3a Using the vi editor

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

15-122: Principles of Imperative Computation

Short Read Sequencing Analysis Workshop

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

CHE3935. Lecture 1. Introduction to Linux

CS1600 Lab Assignment 1 Spring 2016 Due: Feb. 2, 2016 POINTS: 10

CS Fundamentals of Programming II Fall Very Basic UNIX

Lab 1 Introduction to UNIX and C

CSCE 212H, Spring 2008, Matthews Lab Assignment 1: Representation of Integers Assigned: January 17 Due: January 22

Laboratory 1 Semester 1 11/12

CSI33 Data Structures

CS CS Tutorial 2 2 Winter 2018

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

AMS 200: Working on Linux/Unix Machines

Unix Introduction to UNIX

Connecting to ICS Server, Shell, Vim CS238P Operating Systems fall 18

Lab 1 Introduction to UNIX and C

Introduction to Linux

Physics REU Unix Tutorial

Understanding main() function Input/Output Streams

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi and Cyrill Stachniss

Getting Started With UNIX Lab Exercises

Unit 13. Linux Operating System Debugging Programs

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Lab 2: Linux/Unix shell

Lab # 02. Basic Elements of C++ _ Part1

Lab 1: Introduction to C, ASCII ART & the Linux Command Line

Introduction to the Linux Command Line

Introduction to Linux. Fundamentals of Computer Science

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Linux/Cygwin Practice Computer Architecture

Using the Zoo Workstations

Chapter 1 INTRODUCTION

Lab 1 1 Due Wed., 2 Sept. 2015

Tutorial 1: Unix Basics

Mills HPC Tutorial Series. Linux Basics I

Session 1: Accessing MUGrid and Command Line Basics

Chapter 2. Editing And Compiling

C++ Programming on Linux

Introduction to Algorithms and Programming I Lab. Exercises #1 Solution

Hand-on Labs for Chapter 1 and Appendix A CSCE 212 Introduction to Computer Architecture, Spring

Computer Programming : C++

Lab: Supplying Inputs to Programs

Programming Studio #1 ECE 190

Getting started with Hugs on Linux

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

Review of Fundamentals

Tutorial 2: Compiling and Running C++ Source Code

Development Environment & Linux Guide

Chapter-3. Introduction to Unix: Fundamental Commands

Unix Basics. Systems Programming Concepts

Parallel Programming Pre-Assignment. Setting up the Software Environment

Perl and R Scripting for Biologists

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Getting started with Hugs on Linux

4. Structure of a C++ program

Numerical Algorithms for Physics. Andrea Mignone Physics Department, University of Torino AA

Unix Tutorial Haverford Astronomy 2014/2015

CS 261 Recitation 1 Compiling C on UNIX

Working with Basic Linux. Daniel Balagué

Lab 1: Getting Started with Linux The Extremely Illustrated Version. Graham Northup

Operating Systems. Copyleft 2005, Binnur Kurt

Introduction to Programming

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

UNLV Computer Science Department CS 135 Lab Manual

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

EL2310 Scientific Programming

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Repetitive Program Execution

CMSC 201 Spring 2017 Lab 01 Hello World

EL2310 Scientific Programming

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

Linux File System and Basic Commands

CS11 Intro C++ Spring 2018 Lecture 1

Helpful Tips for Labs. CS140, Spring 2015

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 391 Lecture 1. introduction to Linux/Unix environment

ECE 2036 Lab 1: Introduction to Software Objects

Lecture # 2 Introduction to UNIX (Part 2)

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

213/513/613 Linux/Git Bootcamp. Cyrus, Eugene, Minji, Niko

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Linux Survival Guide

CMSC 201 Spring 2018 Lab 01 Hello World

CSC 112 Lab 1: Introduction to Unix and C++ Fall 2009

We first learn one useful option of gcc. Copy the following C source file to your

CISC 220 fall 2011, set 1: Linux basics

Transcription:

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit Contents 1 An Introduction to C++, Unix, SSH and Komodo Edit 1.1 Introduction 1.2 The C++ Language 1.2.1 A Brief Introduction 1.2.1.1 Recommended Reading 1.2.2 Use of C++ 1.3 Relevant Computer Systems History 1.4 Komodo - Creating a C++ Program 1.5 C++ - Program Components 1.6 SSH 1.7 Unix vs Windows Directory Structure 1.7.1 Unix Process Commands 1.7.2 Unix Directory and File Commands 1.8 File Transfer 1.9 Compiling and Executing a C++ Program 1.9.1 The C++ Compilation Process - from Source Code to Executable Code 1.9.2 Compile and Execute the hello.cpp Program 1.10 Unix - The vi Editor 1.11 Using Switches with the clang++ Compiler 1.12 Appendix: Some Useful Linux Commands Introduction This lab will introduce the programming environment used in this course. Topics Covered in this Lab: Brief Introduction to the C++ Language Unix and the GNU C++ compiler SSH and File Transfer Creating, saving, compiling, and executing programs Basic C++ program structure The course website and submitting programs Questions Answered in this Lab: How is a program file opened or created? What does a compiler do with a program? What do the different pieces of a program do? How does a computer program communicate with a user? Demonstrable Skills Acquired in this Lab: The C++ Language A Brief Introduction Ability to create a simple C++ program Ability to compile and run a program Familiarity with console output, specifically C++'s cout Familiarity with Unix, the Unix file system and directories. Development of C++ began in 1979 by Bjarne Stroustrup, then employed by AT&T Bell Labs. His goal was to combine the program organization of Simula, the speed of BCPL programs along with BCPL's ease of linkage and C's efficiency, flexibility and portability. Originally titled C with Classes, the name C++, suggested by Rick Mascitti, was adopted in 1983. By 1988 the number of users and number of implementations of C++ indicated a need for standardization. To start the process, Stroustrup, with assistance from Margaret Ellis, wrote The Annotated C++ Reference Manual which was reviewed by approximately one hundred C++ language users from various organizations. The organizational meeting of the ANSI (American National Standards Institute) C++ committee was held in December of 1989 with international attendees. In June 1991, the ISO (International Organization for Standardization) C++ committee convened with a majority of representatives being C++ programmers. The first ISO/IEC C++ standard was released in 1998. This standard is informally referred to as C++98. While there have been minor revisions since 1998, the language itself was not changed until C++11, the standard released in 2011. This information was gathered from various sources including those listed in the Recommended Reading section below. Recommended Reading Use of C++ http://www.stroustrup.com/c++.html http://www.hitmill.com/programming/cpp/cpphistory.html http://www.cplusplus.com/info/history/ C++ can be used for many diverse applications. The following links provide specific companies and products written partially or entirely in C++. http://www.stroustrup.com/applications.html http://www.lextrait.com/vincent/implementations.html

Relevant Computer Systems History Many of the tools discussed here were originally part of the UNIX operating system, but are now available in other operating systems that provide a UNIX-like interface. For that reason, we will use the term "Unix" (not all uppercase) to refer to any UNIX-like system. These tools have an interesting history that you should read as time permits. In addition, click here to read about Komodo Edit. Komodo - Creating a C++ Program A simple program to write the traditional "Hello, World!" message will be created in this section. 1. Open Komodo Edit. When Komodo opens, click the File menu and choose Save As. Using the drop-down box labeled "Save in", find the folder that is your home directory (or your Student Files folder if you changed to it). This is where you will probably want to save your files. You can copy or move them later if necessary. For now, save the file as hello.cpp to your documents folder. 2. Modify the comments as needed, otherwise, type the following code exactly as it appears. Code Illustration 3. Save the changes. C++ - Program Components In this section, each part of the program is examined in detail. 1. The first section of the program, consisting of the lines starting with "//", is a comment block. This is where the program name, author name, description of what the program is to do, and other related information are to be placed. See the course's documentation guidelines for more information. Comment lines may be placed anywhere deemed useful in the program. When "//" is encountered on a line, the compiler ignores everything on the rest of that line. 2. #include <iostream> This is a preprocessor directive which tells the compiler that information in the header file iostream is needed to make this program work; iostream allows programs to write to the screen and read from the keyboard using the identifiers cout and cin, respectively. 3. using std::cout; This specifies to the compiler where cout is defined; std is a namespace, but it is not necessary to go into more detail at this time. 4. int main () The next line starts the vital part of the program, the function main. This line tells the compiler that the following function is named main and that it will return an integer value. Every console-based program must have a function named main. 5. Braces { and } enclose the body of function main. Everything inside the braces makes up the body of the function. This is where the computer is told what steps to take to solve the problem. 6. cout << "Hello, World!\n"; This line does the significant work of this program. It sends the character string "Hello World!\n" to the standard output stream, cout, which is in effect the display (or "c"onsole "out"put) 1. The stream insertion operator << causes anything to its right to be inserted into the stream on its left, in this case cout. The escape sequence character '\n' indicates the end of a line, which moves the cursor to the beginning of the next line as if the enter key had been pressed when typing. (For more escape sequences, refer to the textbook.) 7. return 0;

This line returns the value zero from this program to the program that initiated it, usually the operating system. The value returned can be used to indicate the completion status of the program. Values other than zero are typically used to indicate an error condition. Every program in this course will return zero. 1Actually, according to [Stroustrup], the "c" in cout means "character"; either mnemonic will work fine though. SSH We will be compiling and executing our programs via a remote server. Historically, a two-way, text-only connection between a local computer and a remote server was accomplished with Telnet. Telnet has no security measures; as a result, passwords are passed in plain-text format for verification. We will access the server for this course in a more secure manner using SSH (Secure Shell). SSH uses encryption so passwords are no longer passed across the Internet in plain-text form. 1. Start MobaXterm (Start, All Programs, MobaXterm, MobaXterm) 2. From the Sessions tab in the left pane, click New session. 3. Click the SSH button, and type or copy the server's address (provided by your instructor) into the 'Host' box 4. Check the box next to 'Specify username' and type your account logon into the 'Specify username' box; this is the first part of your A-State email address, probably firstname.lastname 5. Click OK. This will create an entry in the Sessions pane so that you won't have to enter this information again. 6. Enter your password when prompted. You should now be connected to the remote course server. Before closing a secure shell window, be sure to log off the remote server by executing the command logout. Unix vs Windows Directory Structure The Microsoft term folder corresponds to a directory in Unix. The Unix file system begins at a location called the root. This directory has the path name "/". Everything on the computer system that can be accessed through the filesystem is available within sub-directories starting with root. This is quite different from Windows, where the file system is based on disk drives. Windows paths begin with a drive letter, with sub-directories below it. In Unix, the drives are just parts of the whole system, and are listed below root (exactly where can vary from one system to another). Unix Process Commands In Unix, each running task is considered a process. To view a list of processes, type ps and press Enter. $ ps PID TTY TIME CMD 887 pts/0 00:00:00 bash 909 pts/0 00:00:00 ps The exact list will depend on what tasks are currently running. In the above list, two process, bash and ps, are associated with the current user. ps is the command used to generate the list while bash is the shell in which the command was executed. The bash process has an ID of 887 while the ps process has an ID of 909. If a process "hangs", you can use the kill command to stop the process. As an example, if the process with the ID of 892 needs to be stopped, type kill 892 $ kill 892 Unix Directory and File Commands When you first logon to Unix, it will open up the shell in your "home" directory. In most Unix systems, the "home" directory is "/home/your_username". The pwd command will display the location of the current diretory. Type pwd and press Enter to see what the full path of your home directory is. $ pwd /home/user.name/ The exact directory may differ depending on how Unix is set up. The mkdir command will create a new directory (folder). Enter mkdir sp01 to make a directory named sp01 corresponding to lab 01 for Structured Programming $ mkdir sp01 The ls command will display a listing of the files and directories of the current directory. Enter ls (that is a lowercase L followed by an s) to see a listing of the current directory's files and folders. $ ls sp01 Enter ls sp01 to verify that no files are present in the new directory. The cd command will change to a different directory. Enter cd sp01 to change directories from the current directory to sp01. $ cd sp01 Note that after using cd, the command prompt now says you are in directory sp01 under the previous directory you were in (in this case, the

home directory ~). /sp01 $ _ There is a shortcut name for your home directory. It is "~". You can change directories to your home directory simply by typing "cd" at the command window... This is the same as "cd ~", or "cd" followed by the full path to your home directory. FOR IN-LAB CREDIT: Demonstrate to a lab proctor your ability to logon to the server and change to the sp01 directory. (Logout and close the MobaXterm program before demonstrating this checkpoint.) File Transfer In order to place the file completed earlier with the Komodo Edit software on the remote server, we will need to access the file transfer portion of the MobaXterm software. 1. Start MobaXterm if it isn't already started, or switch to it if it is running. (Start, All Programs, MobaXterm, MobaXterm) 2. From the Sessions tab in the left pane, click New session. 3. Click the SFTP button, and click OK. 4. Type or copy the server's address (provided by your instructor) into the 'Remote hostname' box 5. Type your account logon into the 'Username' box. 6. Click OK. This will create an entry in the Sessions pane so that you won't have to enter this information again. 7. Enter your password when prompted. 8. You should now see a tab with the server on the right (the larger pane) and your local machine on the two panes to the left. You may simply drag-and-drop files from your machine to the server (and vice-versa). 9. Drag-and-drop the hello.cpp file into the sp01 directory on the server. Note: It is also possible to open and save files directly from the Unix server using only Komodo. From the File menu, select Open - Remote File to open a file from a remote server. To save a new file to a remote server, choose File - Save As Other - Remote File. Use Komodo Help (or ask your instructor) to assist you in adding the remote server to the Accounts list. It is still a good idea to keep a copy of the file both on the remote server and in a local storage location. FOR IN-LAB CREDIT: Demonstrate the presence of the hello.cpp file in the sp01 directory to a lab proctor. Compiling and Executing a C++ Program

The C++ Compilation Process - from Source Code to Executable Code The program code typed by the user is called source code and is stored in a text file called the source file. When compilation begins, the preprocessor reads the source code searching for the # symbol; the # symbol indicates a preprocessor directive. There are several preprocessor directives. Research preprocessor directives or review CPlusPlus's Preprocessor Directives for a tutorial. The preprocessor directive demonstrated in the hello.cpp program is the include directive. This directive gives a programmer access to previously written code. The filename specified, in this case iostream, is called a header file. Header files contain declarations of functions and possibly constants that can be used by a program once it has been included. The modified source code is then compiled. The compiler converts the modified source to object code, considered to be machine-language. Any deviation in syntax will confound the compiler; these errors are reported to the programmer and the entire compilation process is halted. Once any syntax errors have been corrected and the object code created, the next step is to link all necessary object code files to create an executable file. This is the job of the linker. Once created, the executable file may be executed as many times as required without the need to recompile. If a change is necessary, the compilation process must be revisited. Compile and Execute the hello.cpp Program Unix supports clang, a free C++ compiler. It can convert the human-readable source code created in the previous section into machine-executable program code. Before submitting any program, you should first verify it will compile and execute correctly; thorough testing is important. When you have finished editing the file, save it (in Komodo), transfer it to the server, then switch to the SSH window. 1. Compile and Link: Compile and link the program with clang++ hello.cpp. If there are no errors, Unix will simply display another prompt. (Most Unix programs are quiet rather than verbose.) If there are errors, seek assistance from a lab proctor. /sp01 $ clang++ hello.cpp If clang++ does not print anything then everything is OK. 2. Display a listing of the current directory's files. The list should include hello.cpp as well as a.out, which is the default name for an executable program. 3. Execute: Enter./a.out (period followed by /a.out) or possibly just a.out to execute the program. (The single period is the path name for the current directory; if it is not present in the system path variable, you must explicitly include it to specify the program's location.) /sp01 $./a.out Hello World! 4. Compile and Link - specify executable filename: Enter clang++ -o speak hello.cpp to give the executable the name speak; execute it with./speak. FOR IN-LAB CREDIT: Demonstrate the speak program for a lab proctor. With a lab proctor present, execute the speak program.

Unix - The vi Editor For the sake of tradition, every computer science student should be familiar with the vi editor, the historic (1975) piece of software that was used by many early Unix programmers to write their programs; there are traditionalists who continue to use vi to this day. (The name vi is pronounced vee-eye; it is an abbreviation of the word visual.) You may wish to review Mastering the vi Editor for detailed instructions on how to use the vi editor. Unlike visual editors such as Windows' Notepad, vi uses character commands to manipulate file content. A minimal set of character commands follows: Command i a Esc :wq :q! u x dd Action insert mode (inserts before the character under cursor) append mode (inserts after the character under cursor) leave (escape) insert or append mode write file to disk and quit (:w means "write", :q means "quit") quit without saving any changes undo (only when not in insert or append mode) delete character under cursor (only when not in insert mode) delete current line Use the vi editor to write the program sp01il.cpp. 1. At the Unix command prompt, move to the sp01 directory, if necessary, and type vi sp01il.cpp and press Enter. /sp01 $ vi sp01il.cpp This command will open the file sp01il.cpp in the vi editor. 2. Press the i key to activate insert mode. 3. Enter the following lines of code, replacing the comments in the main function with appropriate code. (Do not copy/paste.) Do not forget a completely blank line at the end of the file. // ***** sp01il.cpp ***** #include <iostream> using std::cout; int main () { // replace this comment with a statement to display your first name to the screen // replace this comment with a statement to display your last name next to your first name // be sure to include a space to separate the two names. // look to the cout statement in the hello.cpp program to complete these two lines of code return 0; } 4. Press the Esc key to leave insert mode. 5. Type :wq to write the file to disk and exit the vi editor. 6. Compile and execute this program. FOR IN-LAB CREDIT: Demonstrate to a lab proctor the use of the vi editor. With a lab proctor present, open 'sp01il.cpp in the vi editor and add a comment containing today's date at the beginning of the first blank line; save the file and exit the vi editor. Using Switches with the clang++ Compiler For grading purposes, all programs will be compiled using the command clang++ -Wall -Wextra -pedantic -std=c++17 What follows clang++ are called switches. -Wall -Wextra turns on all warning messages, including uncommon ones; you should treat warning messages as errors and attempt to correct the problem. -pedantic increases the sensitivity of the compiler to errors/warnings specified by the C++ ANSI standard. -std=c++17 tells the compiler to use the C++17 standard (the most recent standard available in clang++). Before submitting any program, it should be compiled with the same set of switches. Try compiling hello.cpp by using the following command: /sp01 $ clang++ -Wall -Wextra -pedantic -std=c++17 hello.cpp If clang++ does not print anything then everything is okay. Upload the following files: hello.cpp, sp01il.cpp

Appendix: Some Useful Linux Commands The table below lists a handful of linux commands that you will probably find very useful for code development. You can also find this table at the wiki page Common_Linux_Commands. Command ls pwd cd dir mkdir new_dir rm file cp source dest mv old new cat file more file vim file clear Action list directory contents; add -l for more information print working directory: shows path to your current working location change directory to dir;.. means "parent" make a new directory with name new_dir removes file; add -r to delete a directory copy file source to dest move (or rename) file or directory old to new prints contents of file to terminal same as cat but allows paging of long files command-line editor: edits or creates file clears the screen More information about these commands can easily be found online.