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.
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 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
Your commands will appear to the right of the
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
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
For a simple example of this, try running the
true command, which always
~$ 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.
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.
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.
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++ -std=c++11 my-code.cpp test.cpp -o program-name
This invocation has four components:
You can take the output from one program and send it to a file or to
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
$ 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
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.
There are a few commands that will be essential for our labs. These are commands for interacting with C++ source, files and directories.
In the lab, we will be using the Clang compiler by the name
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
c++ is sometimes defined as the default system compiler:
Clang on Mac OS X or the GNU C++ compiler on Linux.
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
On Windows, this will be a directory like
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
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 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
~/Documents/Teaching/engi3891/lab0$ cd .. ~/Documents/Teaching/engi3891$
To list the files in a directory, use
To create a directory, use
To remove a directory, use
~$ ls Desktop Documents ~$ mkdir tmp ~$ ls Desktop Documents tmp ~$ rmdir tmp ~$ ls Desktop Documents