cheatsheet/cpp-cheatsheet.md

144 lines
3.2 KiB
Markdown

# 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**