Skip to content

Commit 213ec58

Browse files
authored
Merge pull request #166 from UCL/mm_updates2025
Mm updates2025
2 parents 1c5c899 + 0e412a9 commit 213ec58

4 files changed

Lines changed: 60 additions & 4 deletions

File tree

01projects/UsingTheTerminal.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Terminal Commands Cheat Sheet
3+
---
4+
5+
# Terminal Commands Cheat Sheet
6+
7+
You will need to use the terminal on your machine every week for cloning code repositories, compiling code, and running executables. This page provides some of the most common commands that you will need to use in your terminal. Where you see angle brackets such as `<directory>` this means that this is an argument for a command, and should be replaced (in this example, with the name of a directory).
8+
9+
## Basic Terminal Commands
10+
11+
- `pwd`: This displays the _present working directory_. This is where you currently are in the filesystem.
12+
- `cd <directory>`: This is _change directory_, and allows you to move to a different folder. Use `cd ..` to move up one level to the directory that contains the present working directory. You can go up multiple levels by `cd ../..` and so on.
13+
- `ls`: This displays the contents of the current directory. You can write `ls <directory>` to display the contents of a directory that you are not in. If you write `ls -la` instead of just `ls` you will get more information, like when the file was created.
14+
- `mkdir <name>`: Creates a new directory with the given name.
15+
- `cp <source> <destination>`: Copies a source file to a new destination. If the destination is a directory then it will copy the file into that directory and keep the name; if the destination includes a file name then the copy of the file will be given the new name.
16+
- `mv <source> <destination>`: The same as `cp` but it _moves_ the file instead of copying it.
17+
- `rm <file>`: Remove (i.e. delete) a file.
18+
- `rm -rf <directory>`: Remove an entire directory.
19+
- `code <directory>`: Open a folder with VSCode. Use `code .` to open the current directory in VSCode.
20+
21+
## Git
22+
23+
- `git clone <repo>`: Clone a git repo.
24+
- `git add <file>`: Add a file to the current commit.
25+
- `git commit -m <message>`: Finalise a git commit with a given message.
26+
- `git checkout -b <branch_name>`: Create a new git branch.
27+
- `git push`: Push to the repository on the current branch.
28+
- use `git push --set-upstream origin <branch_name>` if this is the first time using a new branch.
29+
- `git rm`: Delete a file and remove it from git tracking.
30+
- `git mv`: Move a file and update git tracking.
31+
32+
## Compiling and Running Programs
33+
34+
These commands are a quick reference for a number of tools that will be introduced over this course. They will be discussed more fully in their respective sections, so do not worry if you do not know what these are and do not expect to be able to use these right away. However, you may find it helpful to come back here to quickly look up useful commands.
35+
36+
- `g++ -o <output> -std=c++17 -I<include_directory> <sources>`: Compile a C++ program using the C++17 standard using `g++`. Use `-g` as well when you want to compile with debug symbols.
37+
- `cmake -B <build_folder>`: Create a build folder and initialise cmake therein.
38+
- `cmake --build <build_folder>`: Build a CMake project in the build folder.
39+
- `./<path_to_executable>`: Run an executable. If the executable takes any arguments then place them after this, e.g. `./my_exe 5 10`.
40+
- `gdb <path_to_exe>`: Run an executable in command line debugger (Ubuntu/WSL).
41+
- `valgrind ./<path_to_exe>`: Run an executable with valgrind.
42+
- `g++ ... -fopenmp`: Compile with OpenMP turned on. (Other arguments omitted for brevity.)
43+
- `OMP_NUM_THREADS=<n_threads> ./<path_to_executable>`: Run an OpenMP program with a given number of threads. (Arguments can be supplied after the exe.)
44+
- `mpic++ -o <output> -std=c++17 -I<include_directory> <sources>`: Compile a C++ MPI program.
45+
- `mpirun -np <n_proc> <path_to_exe>`: Run an MPI program with a given number of processes. (Arguments can be supplied after the exe.)

01projects/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ In this section we also provide notes on how to use the **command line terminal*
1313
- [Introduction to C++ and "Hello World" Program](./sec01IntroToCpp.html)
1414
- [Further C++ Syntax](./sec02CppSyntax.html)
1515
- [C++ Programs with Multiple Files](./sec03MultipleFiles.html)
16+
- [Terminal Commands Cheat Sheet](./UsingTheTerminal.html)
1617
- [Version control with Git](./sec05Git.html)
1718

1819
## Useful References

01projects/sec01IntroToCpp.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Here's a little snippet of C++:
7070
```cpp
7171
#include <iostream>
7272

73-
using namespace std;
73+
using std::cout;
7474

7575
int main()
7676
{
@@ -117,16 +117,23 @@ We'll go into what *exactly* this line is doing later but all you need to know a
117117

118118
The next line is a *using statement*:
119119

120+
```cpp
121+
using std::cout;
122+
```
123+
124+
Classes (types of object) and functions from the standard library are prefaced with `std::`. This is called a _namespace_; it's used to avoid name clashes in large programs, and the standard library especially has a large amount in it with common names like `vector`, `map`, and `array` that could easily be used in other ways by other libraries or parts of your program! If you want to avoid writing `std::` all the time, you can use the `using` to make the class or function available without namespacing. You can also import an entire namespace, for example:
125+
120126
```cpp
121127
using namespace std;
122128
```
123129

124-
Again, we'll explore what this whole line actually does later but the short version is it allows us to access functions and classes inside the standard library without typing `std::` everywhere. I recommend you use it but **only in .cpp files**.
130+
This can avoid writing lists of `using` statements but runs the risk of name clashes. It's not usually good practice to import the entire `std` namespace because the potential for clashes is high, although it can be fine for small programs.
125131

126-
There's also an interesting bit of punctuation here; you have probably noticed the line ends in a semicolon `;`. C++, and many other languages, require this because the language doesn't care about (most) *whitespace*. Technically we can write our whole program all on one line, ignoring all indentation and newlines:
132+
There's also an interesting bit of punctuation here; you have probably noticed the line ends in a semicolon `;`. C++, and many other languages, require this because the language doesn't care about (most) *whitespace*. Technically we can write our whole program all on one line, ignoring all indentation and most newlines:
127133

128134
```cpp
129-
#include <iostream> using namespace std; int main() {cout << "Hello World!\n"; return 0;}
135+
#include <iostream>
136+
using namespace std; int main() {cout << "Hello World!\n"; return 0;}
130137
```
131138
132139
But this looks horrible so we use newlines and indentation in appropriate places to make our code more *readable*.

01projects/sec03MultipleFiles.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,6 @@ If your include file is in a different folder, your need to tell the compiler wh
217217
g++ -o test_f main.cpp function.cpp -Iinclude_folder/
218218
```
219219

220+
### A note on `using` statements
221+
222+
You should generally restrict your use of `using` statements to source (.cpp) files rather than header files. The reason for this is that the `using` statement will be imported along with the rest of your header, so any source file that includes your header will end up using that namespacing, and this can significantly increase the risk of clashes when you don't know where that header end up being imported!

0 commit comments

Comments
 (0)