ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Similar documents
ENCM 339 Fall 2017: Cygwin Setup Help

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Lab: Supplying Inputs to Programs

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

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

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

Getting started with UNIX/Linux for G51PRG and G51CSA

Getting Started with Command Prompts

FILE ORGANIZATION. GETTING STARTED PAGE 02 Prerequisites What You Will Learn

Chapter 4. Unix Tutorial. Unix Shell

Linux File System and Basic Commands

1 Installation (briefly)

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

ENCM 501 Winter 2016 Assignment 1 for the Week of January 25

Introduction to Linux and Supercomputers

Download Free Pictures & Wallpaper from the Internet

Physics 306 Computing Lab 1: Hello, World!

ENCM 501 Winter 2018 Assignment 2 for the Week of January 22 (with corrections)

Introduction to Unix - Lab Exercise 0

Using the Zoo Workstations

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

Intermediate Programming, Spring Misha Kazhdan

CS Fundamentals of Programming II Fall Very Basic UNIX

Unit 13. Linux Operating System Debugging Programs

15-122: Principles of Imperative Computation

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

Introduction to Linux

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

Slide Set 9. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Running Sentaurus on the DOE Network

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Lutheran High North Technology The Finder

Week 1: Introduction to R, part 1

Unix Tutorial Haverford Astronomy 2014/2015

CSCI 161: Introduction to Programming I Lab 1a: Programming Environment: Linux and Eclipse

Scripting Languages Course 1. Diana Trandabăț

Semester 2, 2018: Lab 1

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

Basic Survival UNIX.

CSCI 1100L: Topics in Computing Lab Lab 1: Introduction to the Lab! Part I

CSC209. Software Tools and Systems Programming.

Laboratory 1: Eclipse and Karel the Robot

Using IDLE for

Tiny Instruction Manual for the Undergraduate Mathematics Unix Laboratory

Running Java Programs

Parallel Programming Pre-Assignment. Setting up the Software Environment

Unit 10. Linux Operating System

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

XP: Backup Your Important Files for Safety

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

A Brief Introduction to the Linux Shell for Data Science

Slide Set 8. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Exploring UNIX: Session 3

Keep Track of Your Passwords Easily

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

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

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

Lab 1 Introduction to UNIX and C

CSE115 Lab exercises for week 1 of recitations Spring 2011

Contents Slide Set 9. Final Notes on Textbook Chapter 7. Outline of Slide Set 9. More about skipped sections in Chapter 7. Outline of Slide Set 9

Operating System Interaction via bash

Session 1: Accessing MUGrid and Command Line Basics

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

In this exercise you will practice working with HDFS, the Hadoop. You will use the HDFS command line tool and the Hue File Browser

Section 1: Let s Shake Off the Rust!

CSCI 201 Lab 1 Environment Setup

Burning CDs in Windows XP

Introduction to Programming

Lab 1: Introduction to Linux Networking

B a s h s c r i p t i n g

Introduction to Linux and Cluster Computing Environments for Bioinformatics

CMSC 201 Spring 2017 Lab 01 Hello World

Lab1: Communicating science

COMP s1 Lecture 1

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

eftp Application User Guide

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

Working with Basic Linux. Daniel Balagué

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Tutorial 1: Unix Basics

Spring 2017 Gabriel Kuri

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

Student Guide to Neehr Perfect Go!

Touring the Mac S e s s i o n 4 : S A V E, P R I N T, C L O S E & Q U I T

Setting up my Dev Environment ECS 030

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

CSC209. Software Tools and Systems Programming.

Working with the Command Line

Section 2: Developer tools and you. Alex Mariakakis (staff-wide)

CP Lab 1: Introduction to DICE and the C compiler

Module 1: Introduction RStudio

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Introduction: The Unix shell and C programming

Lab Assignment #1. University of Pittsburgh Department of Electrical and Computer Engineering

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

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Introduction to Linux Environment. Yun-Wen Chen

Practical Session 0 Introduction to Linux

Customizing DAZ Studio

Transcription:

page 1 of 8 ENCM 339 Fall 2017: Editing and Running Programs in the Lab Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2017 Introduction This document is a fairly brief introduction to getting things done in the ENCM 339 lab in the Fall 2017 term. It s aimed mainly at students in lecture section 01, but most of the material here will probably also be useful to students in lecture section 02. 1 Linux, Microsoft Windows, and Cygwin 1.1 Background For many years, the labs used for ENCM 339 had computers running various versions of the Linux operating system. Linux is generally regarded as less convenient than Microsoft Windows or Mac OS X for casual users, but is also seen by many engineers and computer scientists as more powerful, flexible and interesting than the Microsoft and Apple operating systems. Linux is a really important system, for big, expensive computers in server rooms, for small computers in inexpensive electronic devices, and for many other kinds of computers. For these reasons, instructors of ENCM 339 have felt it important to expose students to Linux. In the 2017 18 academic year, Linux won t be available in the ENCM 339 lab. The main reason is that it s expensive for system administrators to maintain more than one operating system, and many, many courses in the School of Engineering use software that runs only on Microsoft Windows. 1.2 Cygwin It s hard to give a short, precise summary of exactly what Cygwin is! For the purposes of ENCM 339, we can say that Cygwin is software that allows you to develop and test software on Microsoft Windows in very much the same way you would do it on Linux. Command-line skills developed in the ENCM 339 lab will definitely be helpful for later courses that use Linux, and for summer jobs or internships where those skills are useful. This is particularly important for Software Engineering students and for Electrical Engineering students considering the Computer Engineering Minor. To use Cygwin in the lab: Double-click on the Cygwin64 icon on the Windows desktop, or use the Windows Start button to search for Cygwin64 Terminal. If you succeed, you should see a window like the one shown in Figure 1, waiting for you to type in a command.

Editing and Running Programs in the ENCM 339 Lab page 2 of 8 Figure 1: Screenshot of a Cygwin Terminal window. This was taken on a machine running Windows 8.1 what you ll see in the lab on Windows 7 will be very similar but not exactly the same. 2 The file system: Boring but important details Attention: The details here are not very exciting but you need to know them well in order to find files quickly and to avoid accidental loss or destruction of your work! 2.1 Drive letters in the lab Microsoft Windows gives different drive letters to each of the file systems accessible to a computer. In our lab... Each computer has its own C: drive, a solid-state drive located within the computer s case. In general, you should not save your files to the C: drive, because those files will not be accessible from any other computers in the lab. You shouldn t count on always using the same computer every time you visit the lab! Each student has his or her own H: drive, which has the same content no matter which School of Engineering computer a student has logged into. (H: drive data is stored on a special-purpose computer called a file server.) Keeping your work on your H: drive is a reasonable choice, as you can log out and leave the lab, then return to the lab and resume work by logging into another computer. If you plug a USB thumb drive into a computer, it will likely appear as the E: drive (but perhaps it may get some other letter). Keeping your work on a thumb drive is another reasonable choice, as long as you are super-careful to always eject the drive safely and take it with you when you leave the lab!

Editing and Running Programs in the ENCM 339 Lab page 3 of 8 2.2 Directory is another word for folder You should already be very familiar with the concept of a folder within a computer s file system. A folder can contain one or more regular files, and can also contain other folders. It s common for a computer user to have to navigate down through several layers of folders to find a particular file of interest. Directory is a term that means the same thing as folder. It s important to know this because commands and documentation belonging to command-line environments such as Cygwin tend to prefer directory to folder. 2.3 Slash and backslash This character is called slash: / And this one is called backslash: \ Make sure you remember the difference! In working with computers, bad things (or maybe just confusing things) will happen if you use one of these characters where the other is required. When working with Cygwin (or with Linux or Mac OS X), slash is the directory separator character. For example, this... /cygdrive/h/encm339/foo/bar.c... specifies the file called bar.c, which is in a folder called foo, which is in a folder called encm339, which is in a folder called h, which is in a folder called cygdrive. (Confusingly, the Microsoft Windows Command Prompt application uses backslash as the directory separator character. In Command Prompt, you would use H:\encm339\foo\bar.c to specify the file given in the earlier Cygwin example. If you ve never heard of Command Prompt, don t worry we won t use it for C programming in ENCM 339. When we do use Command Prompt to run Python scripts later in the course, you ll be given some information about how Command Prompt works.) 2.4 Navigating folders in Cygwin Terminal Inside Cygwin Terminal runs a program called bash. bash is an example of a shell, which is a program designed to accept command lines and run other programs in response to those commands. At any given moment bash has what is called the current working directory, or just the working directory. By default, commands will look for files and directories relative to the working directory. The bash command to identify the working directory is pwd for print working directory. Whenever you re not sure where bash has gone to in the file system, use the pwd command to find out. Cygwin is designed to allow use of a PC running Microsoft Windows as if it were running Linux (or some similar Unix-like operating system). In Linux, there is a root directory at the top of the file system, called simply / (In other words, the symbol for the root directory is just a single slash character.) Any directory or file can be accessed by starting at / and listing directories separated by / characters. For example, in Cygwin, /usr/include/stdio.h

Editing and Running Programs in the ENCM 339 Lab page 4 of 8 is a file called stdio.h, within the directory include, within the directory usr, within the root directory. Cygwin uses a special directory called cygdrive to provide access to all the various lettered drives of a Windows computer. This table of examples should make it obvious how that works: drive Cygwin name C: /cygdrive/c E: /cygdrive/e H: /cygdrive/h We ve seen the pwd command already. Two other useful commands are cd ( change directory ) and mkdir ( make directory ). Let s explain these with a series of examples. Imagine that Alice is a student, using Cygwin in the lab for the first time. When she starts up Cygwin Terminal, the working directory will be her home directory, which is a folder created by Cygwin somewhere on the C: drive. That s not a good place to do work, because the C: drive is only accessible from one computer. So Alice decides to work on her H: drive... cd /cygdrive/h She doesn t want to create a mess of files at the top level of her H: drive, so she makes a folder for ENCM 339: mkdir ENCM339 Note that there is no slash in front of ENCM339 this means that the new folder will be placed within /cygdrive/h rather than in the root directory. Alice s next command is cd ENCM339 now the working directory is /cygdrive/h/encm339. Alice realizes that she doesn t want to put all of her ENCM 339 files in one folder, so she makes a second folder mkdir lab1 then does cd lab1 and then does pwd to check that the working directory is what she expects. The output from pwd is /cygdrive/h/encm339/lab1 Alice is happy and starts downloading some files from D2L into her new lab1 folder. Let s skip all of Alice s work on Lab 1 and imagine her a week later, starting work on Lab 2. She already has an ENCM339 folder on her H: drive, so she doesn t have to make that folder again. But she does want to make a lab2 folder and make that new folder her working directory. Here s one way to do it, with three commands: cd /cygdrive/h/encm339 mkdir lab2 cd lab2

Editing and Running Programs in the ENCM 339 Lab page 5 of 8 2.5 Special directories called. and.. A single dot refers to the current directory. We ll see an important use for that when running executable files we make from C code. Two dots side-by-side refer to the directory one level up from the current directory. So cd.. navigates one level up in the file system. Often it s useful to combine.. with a slash and a regular directory name. For example, suppose Bob has made two directories called exa and exb within his lab3 directory. He s in his exa directory, but wants to get to his exb directory. One way to do it is to use cd.. to get up to his lab3 directory, then cd exb to get down to his exb directory. But a faster way to do it is with one command: cd../exb 2.6 Effect of cd with no argument What happens if you enter the command cd without telling cd where you want to go? It turns out that this is a shortcut for changing to your home directory. That s often useful on Linux systems, but not so great for Cygwin in the ENCM 339 lab, because you probably don t want to work on the C: drive. 2.7 Learning more Click this link to a tutorial (or tell your Web browser to go to linuxcommand.org) to get a good introduction to the basics for working with the bash command line. Some of the information in the tutorial is specific to Linux, but most of the content applies perfectly to Cygwin as well. In particular, please find out how to use these commands: ls cp mv rm list contents of a directory copy files or directories move or rename files or directories delete (remove) files or directories 2.8 Feel free to use Windows Explorer Windows Explorer (sometimes called My Computer ) is a great tool for inspecting your folders and files, moving them around, giving them new names, and so on. It s completely reasonable to use both Windows Explorer and Cygwin Terminal while you re working on ENCM 339 exercises in the lab. 3 IDEs versus text editors 3.1 IDEs IDE stands for integrated development environment. Some important IDEs are: Microsoft Visual Studio, the most commonly used system for development of applications for Microsoft Windows; Apple s Xcode, used to develop applications for Mac OS X and ios;

Editing and Running Programs in the ENCM 339 Lab page 6 of 8 Eclipse, a cross-platform IDE, often used to develop applications in the Java programming language. An IDE provides a single, unified system for many of the tasks involved in developing an application. Here s an incomplete list of those tasks: organizing all of the source files belonging to a project; editing the source code within files; finding and correcting errors that prevent source code from being compiled; building executable files and running them to find out whether programs work as expected. 3.2 We won t use an IDE in the lab Good IDEs are often very powerful and convenient, and your instructors encourage you to learn how to use more than one IDE. However, IDEs tend to hide some of the details about programming that are really useful to know, such as: what kinds of files are involved in writing and testing a C program; what the relationship is between a program and a command you can use in a command-line environment; various ways to get commands to read input data files and write output data files. So what we ll do instead in the lab is: edit source code.c files,.cpp files, and.h files in a program called a text editor; build executable programs and test them using a program called a terminal window. As you work on programs in the lab, you will find yourself working with the terminal for a while, the working with the text editor, then with the terminal again, and so on. (From time to time, you ll need to work with a few other applications: a Web browser, for obvious reasons; Windows Explorer, for finding and managing files and folders; and a word processor for putting together post-lab reports.) 3.3 Geany Geany (pronounced like genie ) is the recommended text editor for work in the ENCM 339 lab. Geany is free, easy to install, available for Windows, Mac OS X and Linux, powerful, and designed to be used for writing code. Here are two handy ways to start Geany in the lab: When creating a brand new.c file, launch Geany by searching for it, starting with the Windows Start button. This will open Geany with a blank text file called untitled. Use Save As... from the File menu to choose a good name for the file and place it in an appropriate folder. Then type in whatever text you need, and save the file again. To edit a file that already exists, first use Windows Explorer to find the folder containing the file. Then right-click on the icon for the file, and choose Open with Geany from the pop-up menu.

Editing and Running Programs in the ENCM 339 Lab page 7 of 8 4 Building and running executable files 4.1 Executable files When you write code in C or C++, you can t ask a computer to simply do what is described in your.c or.cpp files! Instead, you first have to make a thing called an executable file, often called simply an executable. An executable file contains machine instructions, among other things. A machine instruction is a short sequence of 0 s and 1 s that represents a simple command for a computer processor chip. Here are two example instructions: Find a two numbers stored somewhere in the computer, add them together, and store the sum somewhere else. Copy a number from some location in RAM circuits to a spot inside the processor chip. The details of how instructions can be combined to make programs is a topic for ENCM 369, not ENCM 339. 4.2 Using Cygwin Terminal to create an executable Suppose you have edited and saved a C source file called myprog.c, and you want to build an executable and run it. Here are simple steps: In Cygwin Terminal, make sure the working directory is the same folder where you saved myprog.c. In Cygwin Terminal, try this command gcc -Wall myprog.c If the command is successful, there will be no error messages; in fact there will be no messages at all. If there are error messages from gcc, you will have to go back to your text editor to fix the errors. Don t forget to save your changes in the text editor before you return to the terminal to try gcc again! If gcc has succeeded, it will have created an executable called a.exe. To run the executable, use this command in Cygwin Terminal:./a.exe The./ in front of the file name is needed to tell bash, Look in the working directory for an executable file, and run that file as a command. Here are a couple of variations: To run the program, it s enough to use./a as a command. (That works in Cygwin but not in Linux!) You can use the -o option to choose a name different from a.exe for your executable. For example, gcc -Wall myprog.c -o myprog.exe will create an executable called myprog.exe (as long as gcc doesn t find any errors).

Editing and Running Programs in the ENCM 339 Lab page 8 of 8 4.3 What does -Wall mean? It s not strictly necessary to use -Wall, but it s highly recommended. -Wall (pronounced minus W all by experienced gcc users) is an example of a command-line option. A command-line option starts with - (a minus sign), and modifies the behaviour of the command it is given to. In this case, W means warn and all means all of the patterns of C code that gcc realizes are almost certainly mistakes. By consistently using -Wall, you will find many problems in code that wouldn t be caught with plain gcc. 4.4 How to kill a running executable Sooner or later, you will write a buggy program that will go into an infinite loop, or get stuck looking for input that never gets provided. To kill such a program in Cygwin Terminal, use the Ctrl-C key combination hold down the Control key, and press the C key.