Basic skills

You will need a few basic computing skills in Engineering 3891 that you may not have picked up in previous courses like Engineering 1020. You will need to become comfortable using the command line and a text editor in order to compile and run your code.

In Engineering 3891, the command line will be our primary means of interacting with the computer. The command line is a place where we can send commands to the computer and the computer can report results back to us:

Commands are typed into a shell program that runs inside of a Terminal (FreeBSD, Linux, Mac OS X) or Command Prompt (Windows) window. You should check your ability to access the shell on your computer’s platform.

Opening the terminal

We will use Ubuntu Linux 14.04 in the computer lab. Opening a Terminal window there is straightforward:

The Windows command prompt is no more difficult to open, though it is a little less intuitive:

On Mac OS X, we can often use a search-based approach like Ubuntu, but sometimes we need to use a slower method:

The shell

The program that runs in an Ubuntu or Mac OS X terminal window is called Bash:

In the Bash shell program, everything before the $ is part of the computer’s prompt to you: when you type a command, it will appear just to the right of the prompt.

The Windows command prompt provides mostly the same information, although it is laid out differently:

This says that you are using the Users\jon directory of the C: drive. Your commands will appear to the right of the > character.

Executing commands

Whether you are using Linux, Mac OS X, Windows or some other operating system (e.g., FreeBSD), you can interact with the shell program by typing a command and pressing the Return key. The computer will run the command you gave and respond with some text. For example, to run the clang++ C++ compiler and ask what version it is, you would type clang++ --version and press Return:

This shows us that we have version 3.4-1ubuntu3 of the compiler installed.

Many programs (including most compilers) will produce no output when they run successfully. For example, a compiler will output warnings and error messages when there is a problem with your code, but it will say nothing when the code can be compiled correctly. For a simple example of this, try running the true command, which always succeeds:

~$ true
~$

We can see that the true program is done, because the shell shows the prompt asking us for the next command, even though there was no output.

Tab completion

Pro tip: if you start typing a command, you can often have the shell finish the command for you by pressing the Tab key. This because particularly handy when typing long file or directory names.

Recall

If you press the up arrow key, the shell will bring up the last command that you typed. You can press the up arrow repeatedly to go back through the command history.

Compiling C++ code

Different C++ compilers can have different names on different platforms. On the EN3000/3029 Linux installation, we will typically use the Clang compiler by running the command clang++:

$ clang++ -std=c++11 my-code.cpp test.cpp -o program-name

This invocation has four components:

clang++
The name of the compiler. Also see other C++ compilers below.
-std=c++11
Tells the compiler to enable modern C++11 features. This will become increasingly important over the course.
my-code.cpp test.cpp
The names of the C++ files to compile.
-o program-name
The name of the executable application file that the compiler should generate. After compiling successfully, you can execute this program by running ./program-name (or just program-name on Windows).

Redirecting output

You can take the output from one program and send it to a file or to another program. This can be useful when you need to refer to the output again later, e.g., when writing lab reports. To save the output of the svn status command into the text file output.txt:

$ svn status > output.txt

The downside of this redirection is that you don’t get to see the output in the current shell. An even better trick is to redirect the output into a program. For instance, you can send output into the tee program, which both saves its input to a file and writes it to the shell:

$ svn status | tee output.txt

The vertical line is called a pipe, because it’s used to pipe data from one program to another. As you may have guessed, the tee program gets its name from a plumbing T-intersection.

Sequential execution

You can run multiple commands, one after another, by putting the && operator between them, e.g.:

$ c++ my-work.cpp -o test && ./test
<compiles my-work.cpp and then, if successful, runs test>

Note that later commands will only be run if the earlier ones succeed. In this example, test will run if c++ encounters no errors.

Essential commands

There are a few commands that will be essential for our labs. These are commands for interacting with C++ source, files and directories.

Compiling C++ files

In the lab, we will be using the Clang compiler by the name clang++. This command also works on Mac OS X. On Windows, the script from the software page will install the GNU C++ compiler, which you can access by running g++.

The command c++ is sometimes defined as the default system compiler: Clang on Mac OS X or the GNU C++ compiler on Linux.

Changing directories

When you open a terminal and start running a shell, you start out in your home directory. On a UNIX-like system (Linux, Mac OS X, FreeBSD, etc.), this will be a directory like /home/username (abbreviated ~). On Windows, this will be a directory like C:\Users\username. An example of a simple directory hierarchy is shown here:

In this example, the Documents folder contains the Teaching folder: we say that Teaching is under Documents, or that Documents is the parent folder of Teaching. Similarly, Teaching contains engi3891, which contains lab0, students and TAs, etc. If I start out in my home directory, to get to the lab0 folder requires that I change to the Documents folder, then Teaching, etc. I can do this with the cd command:

~$ cd Documents
~/Documents$ cd Teaching
~/Documents/Teaching$ cd engi3891
~/Documents/Teaching/engi3891$ cd lab0
~/Documents/Teaching/engi3891/lab0$

Note that tab completion can be very handy when entering directory names. Alternatively, I can traverse the entire hierarchy in one command:

~$ cd Documents/Teaching/engi3891/lab0
~/Documents/Teaching/engi3891/lab0$

Moving “up” to the “parent directory” is done with cd ..:

~/Documents/Teaching/engi3891/lab0$ cd ..
~/Documents/Teaching/engi3891$

Using directories

To list the files in a directory, use ls. To create a directory, use mkdir. To remove a directory, use rmdir:

~$ ls
Desktop   Documents
~$ mkdir tmp
~$ ls
Desktop   Documents   tmp
~$ rmdir tmp
~$ ls
Desktop   Documents