Hide menu

TDDE18 Programming (C++)

Terminal guide

Introduction

In this course you will have to use a UNIX (Linux) based terminal at some point: if not during the labs, then you definitely have to use a terminal during the exam. To make this a bit easier for you it is highly recommended that you use the terminal as much as possible during the course. Try to avoid shortcuts such as IDE tooling or GUI interfaces.

Being familiar with the UNIX terminal will generally make you a more competent programmer and will help you improve your workflow significantly. This is not to say that all competent programmers use the terminal, nor does it mean that the terminal is the best way for everyone. However, by simply having learned how to use the terminal you gain more options and makes it easier for you to make an informed choice on how to work. Think of it like this: how can you know that the way you are currently working is the best way if you haven't tried any other workflows?

So... What is a terminal?

The terminal is a text interface to the computer system you are working on. Through the terminal you can control your computer in a (generally speaking) more fine-grained way than you can with the operating systems' GUI. The terminal works by issuing commands to the operating system. A terminal has the concept of a working directory: this is the current folder that the terminal is currently operating in.

When issuing commands to the terminal it is quite common that you have to tell the command to use a file in some way or another. This is where the working directory matters. If you want to pass the file test.txt to a certain command then you have to make sure that such a file exists in the working directory. If not, then you likely have to change the working directory (or use a relative path which is covered later).

Traversing the filesystem

By default your terminal will be opened in your home directory which is usually denoted as ~ in filepaths. To see your current working directory you can run the command pwd.

To change current working directory, use the command cd. This is an example of a command that takes arguments. Specifically we have to specify what directory we want to change the working directory to. Like this:

$ pwd
/home/christoffer/
$ cd /home/klas
$ pwd
/home/klas/

You can view the content of your current working directory with the command ls. It will list all files and directories stored in the working directory. To change to a directory in the list use cd like this:

$ ls
directory file.txt
$ cd directory

Here we can see the difference between absolute paths and relative paths. An absolute path starts with / and denotes a path to some file starting from the system root (which is the top-level directory). Example: ~/directory/other/

A relative path on the other hand is a path which implicitly starts at the current working directory. Suppose our working directory is your home directory, i.e. ~. Then a relative path would be directory/other.

Another useful thing to know is that cd .. well move the current working directory closer to the root. For example: if our current working directory is ~/directory/subpath and we run cd .. the current working directory will now be ~/directory. You can include .. anywhere in a filepath to move closer to the root. For example: /home/christoffer/directory/../other is equivalent to /home/christoffer/other

Finally, if you want to return to your home directory you can either issue the command cd ~ or you can simply write cd without any arguments.

Compiling

In this course we will primarily use the terminal for compilation of our C++ programs. We will use the compiler called g++. In order to use the compiler we first need a file containing C++ code. These types of files have the file extension .cc or .cpp. You can create these files with a text editor.

To turn your code into an executable file you must first navigate the terminal to the directory that contains your source code file (.cc or .cpp). After that you compile with the following command: $ g++ file.cc where file.cc is replaced with your filename.

If g++ does not print any output to the terminal, then the compilation was successful. If you now run ls you should see a file called a.out This is the executable file that was created from your code. To run this file, write ./a.out.

Some compilation requires additional flags (or options), these are specified when running your compile command. For example: g++ -std=c++17 file.cc.

To summarize the compilation, here is an example where we have a file ~/code/program.cc that we want to compile:

$ pwd
/home/christoffer
$ cd code
$ ls
program.cc
$ g++ -std=c++17 program.cc
$ ls
a.out program.cc
$ ./a.out
Hello world!

Opening files

Files are usually created and edited with a text editor. The ones we are recommending in this course are:

To open these editors through a terminal you can issue the command:

  • code .
  • emacs file.txt
  • vim file.txt

Cheatsheet

pwd
Print current working directory.
cd
Change the working directory to your home directory.
cd ..
Go back in the directory hierarchy.
cd a
Change working directory to a
g++ a.cc
Compile a.cc to the file a.out.
./a.out
Run the program in the executable file a.out.

Other useful commands

cp a b
Is used to copy file a to the file b.
mv a b
Rename and move file a to b.
cat a
Prints the content of file a to the terminal.
find . -iname "a"
Search the current working directory (and all subdirectories) for a file named a.

Page responsible: Christoffer Holm
Last updated: 2023-08-21