First commit
This commit is contained in:
commit
e5bffd2fe1
143
cpp-cheatsheet.md
Normal file
143
cpp-cheatsheet.md
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
# C++ with Clang & Make — Cheat Sheet
|
||||||
|
|
||||||
|
## Writing C++
|
||||||
|
|
||||||
|
A basic C++ file (`hello.cpp`):
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::cout << "Hello, world!" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Compiling with Clang
|
||||||
|
|
||||||
|
| Command | What it does | Example |
|
||||||
|
|---------|-------------|---------|
|
||||||
|
| `clang++ <file>` | Compile to an executable named `a.out` | `clang++ hello.cpp` |
|
||||||
|
| `clang++ <file> -o <name>` | Compile and name the output | `clang++ hello.cpp -o hello` |
|
||||||
|
| `clang++ -Wall <file>` | Enable **all** warnings (always use this!) | `clang++ -Wall hello.cpp -o hello` |
|
||||||
|
| `clang++ -std=c++17 <file>` | Use a specific C++ version | `clang++ -std=c++20 -Wall prog.cpp -o prog` |
|
||||||
|
| `clang++ -g <file>` | Include debug info (for use with a debugger) | `clang++ -g -Wall hello.cpp -o hello` |
|
||||||
|
|
||||||
|
**Common flags explained:**
|
||||||
|
|
||||||
|
| Flag | Meaning |
|
||||||
|
|------|---------|
|
||||||
|
| `-o <name>` | 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**
|
||||||
107
git-cheatsheet.md
Normal file
107
git-cheatsheet.md
Normal file
@ -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 <url>` | 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 <file>` | 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 <file>` | Discard unstaged changes in a file | `git restore main.cpp` |
|
||||||
|
| `git restore --staged <file>` | 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 <name>` | Create a new branch | `git branch feature-x` |
|
||||||
|
| `git checkout <branch>` | Switch to a branch | `git checkout feature-x` |
|
||||||
|
| `git switch <branch>` | Switch to a branch (newer syntax) | `git switch main` |
|
||||||
|
| `git checkout -b <name>` | Create **and** switch to a new branch | `git checkout -b feature-x` |
|
||||||
|
| `git merge <branch>` | Merge another branch into the current one | `git switch main && git merge feature-x` |
|
||||||
|
| `git branch -d <name>` | Delete a branch (safe) | `git branch -d feature-x` |
|
||||||
|
|
||||||
|
## Working with GitHub (Remote)
|
||||||
|
|
||||||
|
| Command | What it does | Example |
|
||||||
|
|---------|-------------|---------|
|
||||||
|
| `git remote add origin <url>` | 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
|
||||||
|
```
|
||||||
97
wsl-ubuntu-cheatsheet.md
Normal file
97
wsl-ubuntu-cheatsheet.md
Normal file
@ -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 <dir>` | Change directory | `cd Documents`, `cd ~` (home), `cd ..` (up one) |
|
||||||
|
| `mkdir <name>` | Create a new directory | `mkdir projects` |
|
||||||
|
| `rmdir <dir>` | Remove an **empty** directory | `rmdir old_folder` |
|
||||||
|
|
||||||
|
## File Operations
|
||||||
|
|
||||||
|
| Command | What it does | Example |
|
||||||
|
|---------|-------------|---------|
|
||||||
|
| `touch <file>` | Create an empty file | `touch notes.txt` |
|
||||||
|
| `cp <src> <dst>` | Copy a file | `cp notes.txt backup.txt` |
|
||||||
|
| `mv <src> <dst>` | Move or rename a file | `mv notes.txt ~/Documents/` |
|
||||||
|
| `rm <file>` | Delete a file (⚠️ no trash bin) | `rm temp.txt` |
|
||||||
|
| `rm -r <dir>` | Delete a folder and **everything inside** | `rm -r old_project/` |
|
||||||
|
| `cat <file>` | Print a file's contents to the terminal | `cat notes.txt` |
|
||||||
|
| `less <file>` | View a file page-by-page (press `q` to quit) | `less long_file.txt` |
|
||||||
|
| `head <file>` | Show the first 10 lines | `head data.csv` |
|
||||||
|
| `tail <file>` | Show the last 10 lines | `tail -n 20 data.csv` |
|
||||||
|
| `nano <file>` | Edit a file in the terminal | `nano notes.txt` |
|
||||||
|
|
||||||
|
## Viewing & Finding
|
||||||
|
|
||||||
|
| Command | What it does | Example |
|
||||||
|
|---------|-------------|---------|
|
||||||
|
| `file <name>` | Show what type of file something is | `file notes.txt` |
|
||||||
|
| `find <dir> -name <pattern>` | Find files by name | `find ~ -name "*.md"` |
|
||||||
|
| `grep <pattern> <file>` | Search inside files | `grep "error" log.txt` |
|
||||||
|
|
||||||
|
## Permissions
|
||||||
|
|
||||||
|
| Command | What it does | Example |
|
||||||
|
|---------|-------------|---------|
|
||||||
|
| `chmod <mode> <file>` | Change file permissions | `chmod +x script.sh` (make executable) |
|
||||||
|
| `sudo <command>` | 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 <pkg>` | Install a package | `sudo apt install clang` |
|
||||||
|
| `sudo apt upgrade` | Upgrade all installed packages | `sudo apt upgrade` |
|
||||||
|
| `sudo apt remove <pkg>` | Uninstall a package | `sudo apt remove clang` |
|
||||||
|
| `apt search <keyword>` | 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 ~
|
||||||
|
```
|
||||||
Loading…
x
Reference in New Issue
Block a user