forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
106 lines (105 loc) · 16.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>PDR: Tutorial 1: Introduction to UNIX</title>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet" href="../../markdown.css">
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<h1 id="pdr-tutorial-1-introduction-to-unix">PDR: Tutorial 1: Introduction to UNIX</h1>
<p><a href="../index.html">Go up to the Tutorials table of contents page</a></p>
<h2 id="part-i-introduction-and-setting-up-your-unix-environment">Part I: Introduction and setting up your UNIX environment</h2>
<p>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; macOS users should use the second option listed.</p>
<p>We recommended using VirtualBox, a free virtual machine software package. This will allow you to run Linux on your home machine without needing to re-install anything. Directions for setting up and how to use VirtualBox can be found at <a href="virtual-box.html">Tutorial 1: Introduction to UNIX: VirtualBox use</a>. This will require downloading a hard drive image that we provide.</p>
<p>There are other options available:</p>
<ul>
<li>macOS users are already running UNIX, as macOS runs on top of FreeBSD (a version of UNIX). So you can just use the computer you have. HOWEVER, note that you still can't use an IDE (Eclipse, Xcode, etc.). You can download Emacs (an editor) from <a href="http://emacsformacosx.com/">here</a>. From a terminal window, try typing in <code>clang++</code> - if you get "Command not found", you will need to install Xcode from the App Store, and then install the Command Line Tools (under Preferences - Downloads). In newer versions of macOS, after typing <code>clang++</code>, a pop-up should appear asking if you would like to install command line developer tools. Also note that you cannot load up Emacs easily from a command line, but you can load it up as you would any other application.
<ul>
<li>You will need to install Xcode and the command line tools (with the latest version of Xcode, you have to download and install the command-line tools separately)</li>
<li>Note that some software that you will need for this course (in particular, doxygen and x86 code creation) does NOT work well on a Mac, and you will likely need the VirtualBox image for that</li>
</ul></li>
<li>Install a Unix variant on your home machine. There are a number of options: Linux, FreeBSD, Solaris, etc. We use Linux (specifically, the Ubuntu distribution), as it has a large user community, both at the University and outside. Thus, if you run into a problem, the course staff might be able to help you. This will require repartitioning your hard drive, and thus there is a chance of data loss. <strong>THUS YOU NEED TO BACK UP YOUR DATA FIRST!!!</strong> You will have to choose a distribution of Linux to install -- as mentioned above, we use Ubuntu. You can find install guides on the web. Be sure to install the development environments with Linux -- pretty much all of the required software will be included in that.</li>
<li>Windows 10 users have WSL (short for Windows Subsystem for Linux) available, which adds an entirely terminal-based Linux environment directly within Windows, with some limitations. You can follow the instructions to install WSL <a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10">here</a>. <strong>NOTE</strong>: WSL is still an evolving technology, and thus some tools may not work as expected -- in particular, the lldb debugger will not work. Course staff will be less able to assist you if you run into WSL-specific issues.</li>
</ul>
<hr />
<h2 id="part-ii-a-tale-of-two-guis">Part II: A Tale of Two GUIs</h2>
<p>When you install Linux, there are a number of graphical user interfaces -- or window managers -- that one can use. Think of how MS Windows is different than macOS, and you get the idea. There are two relevant ones here.</p>
<p><em>KDE</em> is designed to be similar to Microsoft Windows -- it has the equivalent of the start button in the lower left, which pulls up a menu just like in Windows. People who are both familiar with, and prefer, the Windows interface often also prefer this interface. You can see a screen shot of it <a href="https://kde.org/announcements/plasma-5.12/window-shadows.png">here</a>.</p>
<p><em>Gnome</em> (pronounced guh-NOME) is a user interface that is specific to UNIX machines. The interactions with the GUI are a bit different, but the same concepts. One can interact with Gnome really quickly once one gets used to the new interface. You can see a screen shot of it <a href="https://149366088.v2.pressablecdn.com/wp-content/uploads/2018/04/ubuntu-1804-desktop-screenshot.jpg">here</a>.</p>
<p>The reason this is relevant is that your choice of how you will use a UNIX environment will determine which interface you use. The VirtualBox image has <em>both</em> Gnome and KDE (see below for how to switch between them). KDE is more similar to the Windows interface (a "start" menu in the lower-left, etc.), but many people find Gnome more productive once they get used to it.</p>
<p>With VirtualBox, as was mentioned, you can choose which interface. When you log in, after you click the user name and are about to enter your password, there is a gear icon beneath the password field, and you can use that to select which interface -- the Gnome interface is called "Ubuntu", and the KDE interface is called "Plasma". It will remember that choice, so you don't have to enter it again unless you want to change to another interface.</p>
<hr />
<h2 id="part-iii-a-brief-unix-tutorial">Part III: A (brief!) Unix tutorial</h2>
<p>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 macOS) is often better. Unix is coming more into the mainstream lately with the increased popularity of Linux, as well as the fact that macOS is built upon a Unix operating system (FreeBSD, in particular).</p>
<p>The word "Unix" can mean any Unix-like operating system. There are many available -- Linux, FreeBSD, Solaris, macOS, etc. We will be using the word Unix in this class to mean whichever Unix-like environment we are using for this course.</p>
<p>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!</p>
<h3 id="loading-a-terminal">Loading a terminal</h3>
<p>First, you need to load up a terminal. For those who have used the Windows command prompt, it is a somewhat similar interface.</p>
<ul>
<li>With VirtualBox and Gnome, it's an icon on the toolbar on the left-hand side of the desktop</li>
<li>With Virtualbox and KDE, it's the black icon with a greater-than sign in it on the bottom-left</li>
<li>On macOS, open Spotlight and search for Terminal</li>
</ul>
<h3 id="the-unix-tutorial">The Unix Tutorial</h3>
<p>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.</p>
<ul>
<li><code>ls</code> will show the contents of the current directory. 'ls' stands for 'list', but without the 'i' and the 't'.</li>
<li><code>cd pdr</code> will change to the <code>pdr</code> directory -- in other words, it changes the current working directory to whatever you specify. You should notice the text at the beginning of the line change: the <em>prompt</em> will help you keep track of which directory you are in. To go up a directory, you can enter <code>cd ..</code> (notice the one space and then two periods). Also, you can enter <code>cd -</code> to jump to the previous directory you were in (the Ctrl-Z of directory hopping). Finally, typing <code>cd</code> with nothing after it will move you back to your home directory.</li>
<li>You can make directories by <code>mkdir</code> and remove them by <code>rmdir</code>. This is the same as creating a folder in Windows Explorer. A directory that is not empty cannot be removed. For now, we recommend that any directory names (or file names) only have letters, digits, or the underscore in their names (there are ways around that, but they are complicated).</li>
<li>Remove individual files with <code>rm</code>.</li>
<li>Once we start saving files, we will want them to be somewhere easily accessible from Windows. With VirtualBox, the <a href="virtual-box.html">Tutorial 1: Introduction to UNIX: VirtualBox use</a> page discusses how to transfer files back and forth to the virtual machine.</li>
</ul>
<p>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).</p>
<hr />
<h2 id="part-iv-editing-compiling-and-running-a-c-program">Part IV: Editing, compiling, and running a C++ program</h2>
<p>First, you will need to open a terminal (as in the previous section), from which we will load up an editor. We will be using the Emacs editor for all our examples, but you are welcome to use any editor that is not an integrated development environment (IDE). Whatever you decide to use, know that you will be expected to be familiar with it! This includes keyboard shortcuts.</p>
<p>To load up the editor, enter <code>emacs &</code> into the terminal. The ampersand (<code>&</code>) at the end is very imporant! This tells the terminal to start the <code>emacs</code> process in the background, which means you can continue to use the terminal while emacs is open.</p>
<p>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.</p>
<figure>
<img src="emacs-on-open.png" alt="Emacs on open" /><figcaption>Emacs on open</figcaption>
</figure>
<p>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. To help you with getting used to them, we have provided the shortcuts for the commands below.</p>
<p>The first commands in Emacs that we will learn will use the control key (even on macOS!). For example, the save command is listed as <code>C-x C-s</code>. This means hit Control-x then Control-s. We will be going over Emacs command in more detail later. For now, remember that if you get stuck, hit <code>C-g</code> (Control-g) a few times, and that should un-stick it.</p>
<p>We recommend you create a new directory for the rest of this tutorial (<code>mkdir tutorial</code>). <code>cd</code> into that directory.</p>
<p>Next, let's open up a new file. To do that, go to File->Open (<code>C-x C-f</code>). Enter the name of the file (<code>helloworld.cpp</code>). 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.</p>
<p>Enter the following program -- feel free to cut-and-paste (there is an 'Edit->Paste' menu entry, or <code>C-y</code>). 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.</p>
<pre><code>#include <iostream>
using namespace std;
int main () {
cout << "Hello World!" << endl;
return 0;
}</code></pre>
<p>Save the file (File->Save or <code>C-x C-s</code>). If you switch back to the shell, and do an <code>ls</code>, you should see that file listed.</p>
<p>Next, we need to compile that file. To do so, switch to the shell, and enter the command: <code>clang++ helloworld.cpp</code>. 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.</p>
<p>After a successful compilation, do an <code>ls</code> -- you will see a second file, called <code>a.out</code>. This is the compiled version of that program. To run it, enter <code>./a.out</code>. Note the period and slash before the <code>a.out</code> -- why this is there (and how to get rid of it) we will see later in the semester.</p>
<p>Another useful command is the undo command: <code>C-_</code> (Control-underscore).</p>
<p>Right now your Emacs should look approximately like this (with the helloworld.cpp file loaded):</p>
<figure>
<img src="emacs-with-code.png" alt="Emacs with code" /><figcaption>Emacs with code</figcaption>
</figure>
<p>Now, let's turn on line numbers, which will be very helpful when debugging your code. We've seen control commands (such as <code>C-x C-s</code> for saving a file). Next up are commands called meta-commands, because they use the meta (or escape) key. A meta-command looks like <code>M-x linum-mode</code>. Thus, to enter the command, hit the escape key, RELEASE THE ESCAPE KEY, and hit <code>x</code>. 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 <code>x</code>. At that point, the bottom of your Emacs screen will look like the following:</p>
<figure>
<img src="emacs-meta-command.png" alt="Emacs meta command" /><figcaption>Emacs meta command</figcaption>
</figure>
<p>Note the <code>M-x</code> at the bottom -- Emacs is ready to receive an 'extended' meta command. Type in <code>linum-mode</code>, 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 <code>line</code>, 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 <code>M-x linum-mode</code> as many times as you want -- it will just toggle the display of the line numbers on and off.</p>
<h2 id="summary">Summary</h2>
<p>In summary, you should be familiar with the following Emacs commands:</p>
<ul>
<li><code>C-x C-s</code>: Save the file</li>
<li><code>C-x C-f</code>: Open a file, or create a new file if the file name is not found</li>
<li><code>C-x C-c</code>: Exit Emacs (prompts to save the file if it is not saved)</li>
<li><code>C-g</code>: Stop the current command</li>
<li><code>C-_</code>: Undo</li>
<li><code>M-x linum-mode</code>: Turn on line numbers</li>
</ul>
<p>A good Emacs reference sheet can be found <a href="http://www.gnu.org/software/emacs/refcards/pdf/refcard.pdf">here</a>. 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.</p>
<p>When you are all finished, you should exit Emacs (<code>C-x C-c</code>), and logout (by typing <code>logout</code>).</p>
<p>That's it! You can now edit, compile, and run C++ programs. You are ready to proceed to the pre-lab for lab 1.</p>
</body>
</html>