From e5bffd2fe14e2ca37c26b70db9dafe266efab575 Mon Sep 17 00:00:00 2001 From: Mikael Johansson Date: Sun, 14 Jun 2026 12:14:49 +0200 Subject: [PATCH] First commit --- cpp-cheatsheet.md | 143 +++++++++++++++++++++++++++++++++++++++ git-cheatsheet.md | 107 +++++++++++++++++++++++++++++ wsl-ubuntu-cheatsheet.md | 97 ++++++++++++++++++++++++++ 3 files changed, 347 insertions(+) create mode 100644 cpp-cheatsheet.md create mode 100644 git-cheatsheet.md create mode 100644 wsl-ubuntu-cheatsheet.md diff --git a/cpp-cheatsheet.md b/cpp-cheatsheet.md new file mode 100644 index 0000000..5925de5 --- /dev/null +++ b/cpp-cheatsheet.md @@ -0,0 +1,143 @@ +# C++ with Clang & Make — Cheat Sheet + +## Writing C++ + +A basic C++ file (`hello.cpp`): + +```cpp +#include + +int main() { + std::cout << "Hello, world!" << std::endl; + return 0; +} +``` + +## Compiling with Clang + +| Command | What it does | Example | +|---------|-------------|---------| +| `clang++ ` | Compile to an executable named `a.out` | `clang++ hello.cpp` | +| `clang++ -o ` | Compile and name the output | `clang++ hello.cpp -o hello` | +| `clang++ -Wall ` | Enable **all** warnings (always use this!) | `clang++ -Wall hello.cpp -o hello` | +| `clang++ -std=c++17 ` | Use a specific C++ version | `clang++ -std=c++20 -Wall prog.cpp -o prog` | +| `clang++ -g ` | Include debug info (for use with a debugger) | `clang++ -g -Wall hello.cpp -o hello` | + +**Common flags explained:** + +| Flag | Meaning | +|------|---------| +| `-o ` | Output filename (instead of `a.out`) | +| `-Wall` | Enable most compiler warnings | +| `-Wextra` | Enable even more warnings | +| `-std=c++17` | Use C++17 standard (also `c++11`, `c++14`, `c++20`) | +| `-g` | Include debug symbols (for `gdb` or `lldb`) | +| `-O2` | Optimize for speed | + +### Full compile example +```bash +clang++ -Wall -Wextra -std=c++20 my_program.cpp -o my_program +./my_program +``` + +## Running Your Program + +```bash +./my_program # Run it +./my_program arg1 # Pass a command-line argument +``` + +## Make & Makefiles + +`make` automates your build so you don't have to type long `clang++` commands. + +### Example project structure +``` +project/ +├── Makefile +├── main.cpp +├── helpers.cpp +└── helpers.hpp +``` + +### Minimal Makefile +```make +hello: hello.cpp + clang++ -Wall -std=c++17 hello.cpp -o hello +``` + +**⚠️ The indented line must use a Tab, not spaces.** + +### Building with make +```bash +make # Build the first target +make hello # Build the target named 'hello' +``` + +### Better Makefile with variables +```make +CXX = clang++ +CXXFLAGS = -Wall -Wextra -std=c++20 +TARGET = my_program + +$(TARGET): main.cpp helpers.cpp helpers.hpp + $(CXX) $(CXXFLAGS) main.cpp helpers.cpp -o $(TARGET) + +clean: + rm -f $(TARGET) + +.PHONY: clean +``` + +| Command | What it does | +|---------|-------------| +| `make` | Build the program | +| `make clean` | Delete the compiled program | +| `make -j4` | Build using 4 CPU cores (faster) | + +### Typical Makefile with separate compilation +```make +CXX = clang++ +CXXFLAGS = -Wall -Wextra -std=c++20 +TARGET = my_program +OBJECTS = main.o helpers.o + +$(TARGET): $(OBJECTS) + $(CXX) $(OBJECTS) -o $(TARGET) + +main.o: main.cpp helpers.hpp + $(CXX) $(CXXFLAGS) -c main.cpp -o main.o + +helpers.o: helpers.cpp helpers.hpp + $(CXX) $(CXXFLAGS) -c helpers.cpp -o helpers.o + +clean: + rm -f $(TARGET) $(OBJECTS) + +.PHONY: clean +``` + +## Quick Reference — Common Workflow + +```bash +# 1. Write your code +nano hello.cpp + +# 2. Compile +clang++ -Wall -std=c++20 hello.cpp -o hello + +# 3. Run +./hello + +# Or use a Makefile +make +./hello +``` + +## Tips + +- Always compile with `-Wall` — warnings catch bugs! +- Use `-std=c++20` to get modern C++ features (`auto`, `ranges`, `std::span`, etc.) +- If `clang++` isn't installed: `sudo apt install clang` +- If `make` isn't installed: `sudo apt install make` +- When editing Makefiles: **rules must be indented with a Tab character** diff --git a/git-cheatsheet.md b/git-cheatsheet.md new file mode 100644 index 0000000..058080b --- /dev/null +++ b/git-cheatsheet.md @@ -0,0 +1,107 @@ +# Git Cheat Sheet + +## Setup (do once) + +| Command | What it does | Example | +|---------|-------------|---------| +| `git config --global user.name "Your Name"` | Set your name on commits | `git config --global user.name "Alice"` | +| `git config --global user.email "a@b.com"` | Set your email on commits | `git config --global user.email "alice@example.com"` | + +## Starting a Project + +| Command | What it does | Example | +|---------|-------------|---------| +| `git init` | Turn the current folder into a Git repo | `cd my_project && git init` | +| `git clone ` | Download an existing repo from GitHub etc. | `git clone https://github.com/user/repo.git` | + +## Everyday Workflow + +```text + ┌────────┐ git add ┌────────┐ git commit ┌──────────┐ git push ┌────────┐ + │ Working │ ──────────→ │ Staging │ ────────────→ │ Local │ ──────────→ │ Remote │ + │ Tree │ │ Area │ │ Branch │ │ (GitHub)│ + └────────┘ └────────┘ └──────────┘ └────────┘ +``` + +| Command | What it does | Example | +|---------|-------------|---------| +| `git status` | See what files changed and what's staged | `git status` | +| `git add ` | Stage a file for commit | `git add main.cpp` | +| `git add .` | Stage **all** changed files | `git add .` | +| `git commit -m "message"` | Save staged changes with a message | `git commit -m "Add login feature"` | +| `git log` | Show commit history | `git log --oneline` (compact view) | +| `git diff` | Show unstaged changes (line by line) | `git diff` | + +## Undoing Things + +| Command | What it does | Example | +|---------|-------------|---------| +| `git restore ` | Discard unstaged changes in a file | `git restore main.cpp` | +| `git restore --staged ` | Unstage a file (keep your edits) | `git restore --staged main.cpp` | +| `git commit --amend -m "new msg"` | Fix the last commit message | `git commit --amend -m "Fixed message"` | +| `git reset --hard HEAD` | Throw away **all** uncommitted changes ⚠️ | Use with care! | + +## Branches + +| Command | What it does | Example | +|---------|-------------|---------| +| `git branch` | List branches (`*` = current one) | `git branch` | +| `git branch ` | Create a new branch | `git branch feature-x` | +| `git checkout ` | Switch to a branch | `git checkout feature-x` | +| `git switch ` | Switch to a branch (newer syntax) | `git switch main` | +| `git checkout -b ` | Create **and** switch to a new branch | `git checkout -b feature-x` | +| `git merge ` | Merge another branch into the current one | `git switch main && git merge feature-x` | +| `git branch -d ` | Delete a branch (safe) | `git branch -d feature-x` | + +## Working with GitHub (Remote) + +| Command | What it does | Example | +|---------|-------------|---------| +| `git remote add origin ` | Link your local repo to GitHub | `git remote add origin https://github.com/alice/my-project.git` | +| `git push -u origin main` | Push commits to GitHub (first time) | `git push -u origin main` | +| `git push` | Push commits (after first time) | `git push` | +| `git pull` | Fetch and merge the latest from GitHub | `git pull` | +| `git fetch` | Download updates from GitHub (don't merge) | `git fetch` | + +## Common Scenarios + +### Scenario 1: Save your work +```bash +git add . +git commit -m "Describe what you changed" +``` + +### Scenario 2: Put it on GitHub +```bash +git push +``` + +### Scenario 3: Get latest from GitHub +```bash +git pull +``` + +### Scenario 4: Start a new feature (branch) +```bash +git checkout -b my-new-feature +# ... make changes ... +git add . +git commit -m "Add my new feature" +git push -u origin my-new-feature +``` + +### Scenario 5: Undo changes in a file before committing +```bash +git restore broken_file.cpp +``` + +## Cheat Sheet — Most Used Commands + +```bash +git status # What's going on? +git add . # Stage everything +git commit -m "msg" # Save snapshot +git push # Upload to GitHub +git pull # Download from GitHub +git log --oneline # See history +``` diff --git a/wsl-ubuntu-cheatsheet.md b/wsl-ubuntu-cheatsheet.md new file mode 100644 index 0000000..718304a --- /dev/null +++ b/wsl-ubuntu-cheatsheet.md @@ -0,0 +1,97 @@ +# WSL / Ubuntu Command-Line Cheat Sheet + +## Filesystem Navigation + +| Command | What it does | Example | +|---------|-------------|---------| +| `pwd` | Print the current directory (where you are) | `pwd` → `/home/alice` | +| `ls` | List files and folders | `ls`, `ls -la` (detailed view) | +| `cd ` | Change directory | `cd Documents`, `cd ~` (home), `cd ..` (up one) | +| `mkdir ` | Create a new directory | `mkdir projects` | +| `rmdir ` | Remove an **empty** directory | `rmdir old_folder` | + +## File Operations + +| Command | What it does | Example | +|---------|-------------|---------| +| `touch ` | Create an empty file | `touch notes.txt` | +| `cp ` | Copy a file | `cp notes.txt backup.txt` | +| `mv ` | Move or rename a file | `mv notes.txt ~/Documents/` | +| `rm ` | Delete a file (⚠️ no trash bin) | `rm temp.txt` | +| `rm -r ` | Delete a folder and **everything inside** | `rm -r old_project/` | +| `cat ` | Print a file's contents to the terminal | `cat notes.txt` | +| `less ` | View a file page-by-page (press `q` to quit) | `less long_file.txt` | +| `head ` | Show the first 10 lines | `head data.csv` | +| `tail ` | Show the last 10 lines | `tail -n 20 data.csv` | +| `nano ` | Edit a file in the terminal | `nano notes.txt` | + +## Viewing & Finding + +| Command | What it does | Example | +|---------|-------------|---------| +| `file ` | Show what type of file something is | `file notes.txt` | +| `find -name ` | Find files by name | `find ~ -name "*.md"` | +| `grep ` | Search inside files | `grep "error" log.txt` | + +## Permissions + +| Command | What it does | Example | +|---------|-------------|---------| +| `chmod ` | Change file permissions | `chmod +x script.sh` (make executable) | +| `sudo ` | Run a command as admin (root) | `sudo apt update` | + +## Package Management (apt) + +| Command | What it does | Example | +|---------|-------------|---------| +| `sudo apt update` | Refresh the list of available packages | Run before installing anything | +| `sudo apt install ` | Install a package | `sudo apt install clang` | +| `sudo apt upgrade` | Upgrade all installed packages | `sudo apt upgrade` | +| `sudo apt remove ` | Uninstall a package | `sudo apt remove clang` | +| `apt search ` | Search for a package | `apt search image editor` | + +## Pipes & Redirection + +| Command | What it does | Example | +|---------|-------------|---------| +| `cmd1 \| cmd2` | Send output of `cmd1` into `cmd2` | `ls \| grep ".txt"` | +| `>` | Redirect output to a file (overwrite) | `echo "hello" > hello.txt` | +| `>>` | Redirect output to a file (append) | `echo "world" >> hello.txt` | + +## WSL-Specific Tips + +- Your **Windows files** are under `/mnt/c/` — e.g. `/mnt/c/Users/YourName/Documents` +- Your **Linux home** is at `/home/yourname` (type `~` as a shortcut) +- Drag a file from Windows Explorer into the terminal to paste its WSL path +- Use `explorer.exe .` to open the current Linux folder in Windows File Explorer + +## Useful Shortcuts + +| Shortcut | What it does | +|----------|-------------| +| `Ctrl + C` | Cancel the current command | +| `Ctrl + D` | Exit the terminal / close session | +| `Tab` | Auto-complete file and folder names | +| `↑` / `↓` | Scroll through command history | +| `Ctrl + L` | Clear the screen | + +## Quick Reference — First Steps + +```bash +# Where am I? +pwd + +# What's in this folder? +ls -la + +# Make a project folder and go inside +mkdir my_project +cd my_project + +# Create a file and edit it +touch hello.txt +nano hello.txt + +# Come back home +cd ~ +```