Table Of Contents

Previous topic

Interacting via the Terminal

Next topic

The C++ Programming Language

UNIX Tools

The GNU Compiler Collection

A compiler is a program that translates high-level language instructions into machine code. GCC, the GNU Compiler Collection, can translate C, C++, Objective-C, Fortran, Java, and Ada. It also provides code libraries for these languages. In this course, we will be using GCC’s C++ compiler, which is accessed using the g++ command. (Intel’s C++ compiler icc is also installed. This produces somewhat faster executables.)

The C programming language was developed by Dennis Ritchie at Bell Labs in 1972. Object-oriented features were added by Bjarne Stroustrup in 1979 under the name C++ (a joke: ++ is C’s increment operator). Since then, C++ has undergone many refinements, and it has become the most common language for application development.

Online references:

Example

$ cat > hello.cpp
#include <iostream>
using std::cout;
using std::endl;

int main()
{
   cout << "Hello World!" << endl;
   return 0;
}
[ctrl-d]
$ g++ -o hello hello.cpp
$ ls -F
hello*    hello.cpp
$ ./hello
Hello World!

Let us work through the program hello.cpp step by step. Lines that begin with the hash symbol (#) are not part of the C++ language; rather, they are preprocessor directives. In this case, #include tells the compiler to read in the header file iostream, which contains definitions related to input and output.

C++ supports a feature called namespaces. Objects and functions provided as part of the standard library live in the std namespace. The statement using std::name indicates that name should be added to the common namespace. (We could have left out the using std::cout declaration and simply refered to std::cout throughout the program.) cout is a handle to the Unix stdout stream. The desired output—here, the character string "Hello World!" and the line feed endl—are chained together using the << operator.

Every C++ program must contain one function called main. This is the first and only function executed. Once it completes, it gives control back to the operating system and returns an integer, with zero indicating normal operation and other integers denoting various error states.

g++ invokes the gcc C++ compiler, which processes the file hello.cpp. The option -o hello indicates that the compiler should produce an executable file named hello. If the -o option is not included, the executable is named a.out by default.

Make files

The make program provides a mechanism for automating the compilation of your programs. It supports user-defined rules and can track changes to your code based on the date stamp of the files you declare as dependencies.

Online references:

Example

$ cat > makefile
hello: hello.cpp
     g++ -o hello hello.cpp
[ctrl-d]
$ make
make: `hello' is up to date.
$ touch hello.cpp
$ make
g++ -o hello hello.cpp
$ ./hello
Hello World!

Example

$ cat > makefile
CC=g++
CFLAGS=-O2 -ansi -pedantic

hello: hello.cpp

.cpp:
     $(CC) -o $@ $@.cpp $(CFLAGS)

clean:
     rm hello 2> /dev/null
[ctrl-d]
$ make
$ touch hello.cpp
$ make
make: `hello' is up to date.
$ make clean
rm hello 2> /dev/null
$ make
g++ -o hello hello.cpp -O2 -ansi -pedantic
$ ./hello
Hello World!

Emacs

The cat > trick is a quick and dirty way to enter text into file, but in general you will want to use a more full-featured text editor when writing code. Emacs is a popular choice. It provides syntactic colour highlighting and other goodies. It also has a very good built in help system. To access its tutorial type ‘[ctrl-h]’ and then ‘t’.

Wherever in these notes you see cat > filename ... [ctrl-d], you could equally use emacs filename to enter the data into the file.:

$ emacs &
$ emacs mycode.cpp &

Online references:

Example

$ mkdir hello
$ cd hello
$ emacs hello.cpp
#include <iostream>
using std::cout;
using std::endl;

int main()
{
   cout << "Hello World!" << endl;
   return 0;
}
[ctrl-x][ctrl-s]
[ctrl-x][ctrl-f]makefile
hello: hello.cpp
[tab]g++ -o hello hello.cpp
[ctrl-x][ctrl-s]
[ctrl-x][ctrl-c]
$ make
g++ -o hello hello.cpp
$ ./hello
Hello World!

In this example, you create a file named hello.cpp and then save it with [ctrl-x][ctrl-s]. You create another file with [ctrl-x][ctrl-f]makefile. You save that file and then quit emacs with [ctrl-x][ctrl-c]. Emacs maintains a separate buffer for each open file. You can switch between buffers with [ctrl-x][ctrl-b].

Gnuplot

Gnuplot provides both an interactive plotting environment and a scripting system for producing data plots. The output can be sent to the screen or to disk in a variety of graphics formats.

Online references:

Example

$ whereis gnuplot
/bin/gnuplot
$ cat > linear.dat
1 3.1 0.3
2 4.9 0.5
3 6.95 0.4
4 9.2 0.7
[ctrl-d]
$ cat > plot.gp
#!/bin/gnuplot -persist
f(x) = f0 + f1*x
fit f(x) "linear.dat" using 1:2:3 via f0,f1
plot[0:5] "linear.dat" using 1:2:3 with errorbars, f(x)
[ctrl-d]
$ gnuplot -persist plot.gp
$ chmod +x plot.gp
$ ./plot.gp

Example

$ gnuplot
gnuplot> set terminal postscript eps
Options are 'eps noenhanced monochrome blacktext \
   dashed dashlength 1.0 linewidth 1.0 defaultplex \
   palfuncparam 2000,0.003 \
   butt "Helvetica" 14'
gnuplot> set output 'linear.eps'
gnuplot> plot[0:5][0:10] "linear.dat" using 1:2 with lines
gnuplot> unset output
gnuplot> set terminal x11
gnuplot> replot
gnuplot> quit
$ ls
linear.dat      linear.eps
$ gv linear.eps &

The using specification determines which columns in the data file are plotted. In the examples above, using 1:2:3 with errorbars plots column 2 (y-axis) versus column 1 (x-axis) with column 3 acting as the error on each point. It is also possible to perform mathematical transformations on the data. Such transformations are enclosed in parentheses and a dollar sign precedes the column number: e.g., using (1.0/$1):($2+0.5*$3) would plot the value in column 2 plus half the value in column 3 versus the inverse of the value in column 1.

Another popular graphing environment is xmgrace. Some students may prefer its more intuitive, menu-driven interface.

Online references:

GhostScript

PostScript is a page description language from Adobe. It is the native language of most laser printers and is still the most common way to encode printed graphics. In contrast with the Portable Document Format (PDF), which is derived from PostScript, PostScript is a true programming language.

Online references:

A PostScript file always begins with the two-character identifier %! and ends with the showpage command. PostScript files can be viewed using GhostView (gv), which is a graphical front end for GhostScript, the GNU PostScript interpreter.

Example

$ cat > figure.ps
%!
newpath
100 200 moveto
200 250 lineto
100 300 lineto
2 setlinewidth
stroke

newpath
300 300 75 0 180 arc
closepath
gsave
0.5 setgray
fill
grestore
4 setlinewidth
1 0 0 setrgbcolor
stroke

showpage
[ctrl-d]
$ gv figure.ps &

If you are using Linux with the GNOME desktop, you may prefer evince to gv. Under MacOS X, the open command will send PostScript files to Preview.app for viewing.

Encapsulated PostScript (EPS) is a restricted version of PostScript that was designed to make it easier to embed PostScript graphics in other applications. There are three rules to follows. The file should consist of just one page. The first line of the file should be %!PS-Adobe-3.0 EPSF-3.0. There must be a correctly formed BoundingBox comment, of the form %%BoundingBox: xmin ymin xmax ymax, which tells the application that embeds the EPS file how large the image is.

Example

$ echo %\!PS-Adobe-3.0 EPSF-3.0 > figure.eps
$ echo %%BoundingBox: 50 150 400 400 >> figure.eps
$ cat figure.ps | sed 's/showpage//' | sed 's/%\!//' >> figure.eps
$ more figure.eps
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 50 150 400 400

newpath
100 200 moveto
200 250 lineto
100 300 lineto
2 setlinewidth
stroke

newpath
300 300 75 0 180 arc
closepath
gsave
0.5 setgray
fill
grestore
4 setlinewidth
1 0 0 setrgbcolor
stroke
[ctrl-d]
$ gv figure.eps &

ImageMagick

Process images or transform between file formats using the convert command.

Online references:

LaTeX

LaTeX is a document markup language built on top of the TeX typesetting system. It is used by scientists and mathematicians to lay out documents that combine text, mathematics, and figures.

Online references:

Example

$ cat > assignment1.tex
\documentclass{article}
\usepackage{amsmath,amssymb,graphicx}

\begin{document}

\title{PHYS 234/420/580 --- Assignment 1}
\author{Student's name}
\date{Thursday, September 18, 2008}
\maketitle

\section{Statement of the problem}

We are asked to compute the series
\[ S_N = \sum_{n=1}^N \frac{1}{n^2} \]
in the limit $N \to \infty$.

\section{Solution and implementation}

blah blah blah

\includegraphics{figure.eps}

blah blah blah

\end{document}
[ctrl-d]
$ ls
assignment1.tex
$ latex assignment1.tex
$ ls
assignment1.tex           assignment1.dvi
assignment1.aux          assignment1.log
$ xdvi assignment1.dvi &
$ dvips -o assignment1.ps assignment1.dvi
$ gv assignment1.ps &

Archival and compression

The tar (Tape ARchive) command dates back to the days when computer memory was backed up to tape. Nonetheless, this command remains useful as a tool for archiving directories. An archive is a single file that encodes the many files and subdirectories that a directory may contain. Archives can be created and extracted using tar cf and tar xf. To reduce the file’s size, archives are commonly compressed using gzip to produce a so-called tarball, which carries a tar.gz file extension (sometimes reduced to tgz).

Example

$ mkdir beach_1
$ touch beach_1/report.tex
$ touch beach_1/mycode.cpp
$ touch beach_1/output.dat
$ ls
beach_1/
$ ls beach_1
mycode.cpp      output.cpp      report.tex
$ tar cf beach_1.tar beach_1
$ ls
beach_1/        beach_1.tar
$ gzip beach_1.tar
$ ls
beach_1/        beach_1.tar.gz
$ rm beach_1/*
$ rmdir beach_1
$ ls
beach_1.tar.gz
$ gunzip beach_1.tar.gz
$ tar xf beach_1.tar
$ ls
beach_1/        beach_1.tar.gz
$ ls beach_1
mycode.cpp      output.cpp      report.tex

In this example, touch simply creates an empty file with the given filename. Remember that * is a wildcard. The command rm beach_1/* instructs the computer to delete all files inside the beach_1 directory.

Note that the gzip/gunzip steps can be performed automatically by invoking tar with the z option:

$ ls beach_1
mycode.cpp      output.cpp      report.tex
$ tar czf beach_1.tar.gz beach_1
$ ls
beach_1/        beach_1.tar.gz
$ rm beach_1/*
$ rmdir beach_1
$ ls
beach_1.tar.gz
$ tar xzf beach_1.tar
$ ls
beach_1/        beach_1.tar.gz
$ ls beach_1
mycode.cpp      output.cpp      report.tex