PDR: Tutorial 1: Introduction to UNIX

Go up to the Tutorials table of contents page

Part I: Introduction and setting up your UNIX environment

This tutorial is meant to get you up and running with editing, compiling, and executing a program in a Unix-like environment. There are a number of options that you can use to do this, depending on if you have your own PC, and how much time you want to spend installing software. We recommend the first of the options listed if you are using Windows; Mac OS X users should use the second option listed.

The options are:

Should you not want to install anything on your machine, or you want to work elsewhere, there are other options available to you as well:


Part II: A (brief!) Unix tutorial

Unix is a very powerful operating system that has been around, in one form or another, for almost 40 years. The fact that it still exists attests to how powerful it can be for completing tasks on a computer. The reason it is not more widely adopted is because it is not very easy to use or intuitive -- it was written by computer programmers for computer programmers. While the people in this course are certainly capable of learning it, many people just want a computer to work, and to run an Office suite and a set of Internet programs such as a browser. For those users, an easier to use operating system (such as Windows or Mac OS X) is often better. Unix is coming more into the mainstream lately with the increased popularity of Linux, as well as the fact that Mac OS X is built upon a Unix operating system (FreeBSD, in particular).

The word "Unix" can mean any Unix-like operating system. There are many available -- Linux, FreeBSD, Solaris, Mac OSX, etc. We will be using the word Unix in this class to mean whichever Unix-like environment we are using for this course.

A quick note: Unix IS CASE SENSITIVE. Thus, foo, Foo, FOO, and FoO are all different, and allowable, file names. This causes problems with Windows, which sees all those names as the same thing. So be careful about your cases!

Connecting to Unix

First, you need to load up a command shell. For those who have used DOS, or the Windows command prompt, it is a somewhat similar interface.

A Unix system installed on your home machine, including VirtualBox: In Linux (or FreeBSD, Solaris, etc.): Log in (presumably to the X-Windows interface), and load up a command shell. How you load that up is dependent on your version of Linux. With VirtualBox, it's one of the icons on the top toolbar.

A Linux machine in Olsson 001: You will need to reboot the computer into Linux -- see the directions in the first lab for how to do this.

The Unix Tutorial

Here is a quick primer of basic Unix commands. Try each of these out. The idea is for you to be able to manipulate files and directories -- the full Unix tutorial starts next week.

That's it for the Unix tutorial for now. There are many further tutorials online, feel free to read those. You can do all your editing in your desktop folder -- this way, if you want to rename or move your files, you can use Windows Explorer to do so (there are Unix ways to do it, and we'll get to those later).


Part III: Editing, compiling, and running a C++ program

First, you will need to load up a command shell (as in the previous section), from which we will load up the editor. We will be using a version of Emacs for this course.

For this course, you will need to be familiar with Emacs. You are welcome to use any editor that is not an IDE (Integrated Development Environment), but I will expect that you are very familiar with Emacs, or optionally Vim, for the exams. This includes the keyboard shortcuts!

To load up the editor:

You should now have emacs loaded and running -- if not, then something is wrong. If you are using the VirtualBox image, it will look like the following. Other systems will look similar.

Emacs screenshot

Emacs screenshot

All of the mouse commands in Emacs have keyboard shortcuts. Eventually, you will want to learn the shortcuts, as they are much faster to enter once they are known. For the commands below, the keyboard shortcut commands are listed -- partly so you can get used to seeing and using them, and partly because for those using SecureCRT, they cannot use the mouse.

The first commands in Emacs that we will learn will use the control key. For example, the exit command (to leave emacs) is listed as C-x C-c. This means hit Control-x then Control-c. We will be going over Emacs command in more detail later. For now, remember that if you get stuck, hit C-g (Control-g) a few times, and that should un-stick it.

We recommend you create a new directory in your desktop for the rest of this tutorial (mkdir tutorial). Either way, cd into that directory.

Next, let's open up a new file. Do do that, go to File->Open (C-x C-f). Enter the name of the file (helloworld.cpp). Note that if the file does not exist, then it is created (thus, it is like File->New in other programs). If the file does exist, it is opened (like File->Open in other programs). Also note that in the lower part of the Emacs screen, it should now say "C++ Abbrev" -- this is the mode that Emacs is in. The "C++ Abbrev" means that it is in C++ mode, which is what we want.

Enter the following program -- feel free to cut-and-paste (there is a 'Edit->Paste' menu entry). This is the same program found in the first set of slides. What all this C++ code means, we'll get to later -- this tutorial is just for entering, compiling, and running the programs.

// C++
#include <iostream>
using namespace std;
int main() {
    cout << "Hello World!" << endl;
    return 0;
}

Save the file (File->Save or C-x C-s). If you switch back to the shell, and do an ls, you should see that file listed.

Next, we need to compile that file. To do so, switch to the shell, and enter the command: clang++ helloworld.cpp. It will compile, and if successful, the command prompt will be displayed after a brief pause. If there were errors, then the it will list them to the screen.

After a successful compilation, do an ls -- you will see a second file, called a.exe (a.out if you are using Linux, OS X, or one of the lava machines). This is the compiled version of that program. To run it, enter ./a.exe (or ./a.out). Note the period and slash before the a.exe -- why this is there (and how to get rid of it) we will see later in the semester.

Another useful command is the undo command: C-_. This means hold down the control and shift keys, and hit the dash/underscore key.

Right now your Emacs should look approximately like this (with the helloworld.cpp file loaded):

Emacs screenshot

Emacs screenshot

The following is useful for older versions of Emacs. Read over it and familiarize yourself with it, specifically the meta-commands, but note that if you see text coloring and line-numbers, you are good to go.

There are two more Emacs commands that will be very useful as we continue in the course. The Emacs editor is very powerful, but you would never know it by looking at the code that you just entered. It really just looks like Notepad right now:

Emacs screenshot

Emacs screenshot

Let's color the text and turn on line numbers -- both very useful things to have when you are editing code. We've seen control commands (such as C-x C-s for saving a file). Next up are commands called meta-commands, because they use the meta (or escape) key. A meta-command looks like M-x linum-mode. Thus, to enter the command, hit the escape key, RELEASE THE ESCAPE KEY, and hit x. Note that with the control commands, you hold down the control key while pressing the other key -- with meta commands, you press and then release the escape key, and then hit x. At that point, the bottom of your Emacs screen will look like the following:

Emacs screenshot

Emacs screenshot

Note the M-x at the bottom -- Emacs is ready to receive an 'extended' meta command. Type in linum-mode, hit Enter, and the line number that the cursor is on will appear in the status bar at the bottom of the Emacs screen. Note that it is going to be annoying to have to type all of that in each time. This, if you hit the tab key after entering line, it will complete the rest of the command for you. While this may seem like a lot of typing, once you get used to it, these commands can be entered significantly faster than the mouse clicks needed to do this in other editors. There are ways you can have line numbers always displayed when you enter Emacs -- we'll get to these later in the semester. Note that you can enter M-x linum-mode as many times as you want -- it will just toggle the display of the line numbers on and off.

The other command that is useful is to color the program text. Use the command M-x font-lock-mode. Note that if you hit the tab key after entering font, it doesn't fully complete it (you still have to enter 'mode'). Your C++ program should now be colored, and your Emacs window should look like the following.

Emacs screenshot

Emacs screenshot

In addition to the font coloring of the C++ program code, note that the word 'Font' is between 'C++' and 'Abbrev' on the status bar (as we are now using font colors), and the line number is shown to the right of that (the cursor is on line 1 in the image).

Summary

In summary, you should be familiar with the following Emacs commands:

A good Emacs reference sheet can be found here. This file is also in the Collab Resources section, under the misc folder. Note that this page is formatted for A4 paper -- it should print out fine on our letter-sized paper, but you may have to scale the image in Acrobat Reader when you print it out.

When you are all finished, you should exit Emacs (C-x C-c), and logout (by typing logout).

That's it! You can now edit, compile, and run C++ programs. You are ready to proceed to the pre-lab for lab 1.