An open API service indexing awesome lists of open source software.

https://github.com/dconco/cpp-template

A modern, blazing-fast C++ template project with CMake and Ninja build system.
https://github.com/dconco/cpp-template

Last synced: 4 months ago
JSON representation

A modern, blazing-fast C++ template project with CMake and Ninja build system.

Awesome Lists containing this project

README

          

# ๐Ÿš€ C++ Template Project

![C++](https://img.shields.io/badge/C%2B%2B-00599C?style=for-the-badge&logo=c%2B%2B&logoColor=white)
![CMake](https://img.shields.io/badge/CMake-064F8C?style=for-the-badge&logo=cmake&logoColor=white)
![Ninja](https://img.shields.io/badge/Ninja-1F2937?style=for-the-badge&logo=ninja&logoColor=white)

_A modern, blazing-fast C++ template project with CMake and Ninja build system_

[๐Ÿ“‹ Features](#-features) โ€ข [๐Ÿ› ๏ธ Setup](#-setup) โ€ข [๐Ÿ”ง Building](#-building) โ€ข [โšก Quick Commands](#-quick-commands)

---

## โœจ Features

- ๐Ÿ—๏ธ **Modern CMake** configuration
- โšก **Ninja build system** for lightning-fast builds
- ๐Ÿ› **Debug & Release** configurations
- ๐Ÿ‘€ **Auto-rebuild** on file changes
- ๐Ÿ”„ **Multi-core** compilation support
- ๐Ÿ“ฆ **Ready-to-use** project structure

## ๐Ÿ› ๏ธ Setup

### Prerequisites

| Tool | Ubuntu/Debian | macOS | Windows |
| ----------------- | ---------------------------------- | ------------------------ | ---------------------------------------------------------------- |
| **Build Tools** | `sudo apt install build-essential` | `xcode-select --install` | Visual Studio |
| **CMake** | `sudo apt install cmake` | `brew install cmake` | [cmake.org](https://cmake.org) |
| **Ninja** | `sudo apt install ninja-build` | `brew install ninja` | [GitHub Releases](https://github.com/ninja-build/ninja/releases) |
| **File Watching** | `sudo apt install inotify-tools` | Built-in | Built-in |

### Quick Install (Ubuntu/Debian)

```bash
sudo apt update && sudo apt install -y build-essential cmake ninja-build inotify-tools
```

## ๐Ÿ”ง Building

### ๐Ÿ“ Initial Setup

```bash
# Clone your project
git clone https://github.com/dconco/cpp-template.git
cd cpp-template

# Create build directory
mkdir build && cd build
```

### ๐Ÿƒโ€โ™‚๏ธ Build Commands

๐ŸŽฏ First Time Build

```bash
cmake -G Ninja .. && ninja -j$(nproc) && ./app
```

**What this does:**

- ๐Ÿ”ง Configures project with Ninja generator
- ๐Ÿ—๏ธ Builds using all CPU cores
- โ–ถ๏ธ Runs your application

_Use this for initial setup or when CMakeLists.txt changes_

โšก Quick Rebuild

```bash
ninja -j$(nproc) && ./app
```

**What this does:**

- ๐Ÿ”„ Rebuilds only changed files
- โ–ถ๏ธ Runs your application

_Fastest option for regular development_

๐Ÿ› Debug Build

```bash
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug .. && ninja -j$(nproc) && ./app
```

**What this does:**

- ๐Ÿ› Enables debug symbols
- ๐Ÿšซ Disables optimizations
- ๐Ÿ” Perfect for debugging with GDB/IDE

๐Ÿš€ Release Build

```bash
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release .. && ninja -j$(nproc) && ./app
```

**What this does:**

- โšก Full optimizations enabled
- ๐Ÿ“ฆ Smaller executable size
- ๐ŸŽ๏ธ Maximum performance

๐Ÿงน Clean Build

```bash
ninja clean && ninja -j$(nproc) && ./app
```

**What this does:**

- ๐Ÿ—‘๏ธ Removes all build artifacts
- ๐Ÿ”„ Forces complete rebuild
- ๐Ÿฉน Fixes corrupted build cache

๐Ÿ‘€ Watch Mode

```bash
while inotifywait -e modify,move,create,delete ../src/; do ninja -j$(nproc) && ./app; done
```

**What this does:**

- ๐Ÿ‘๏ธ Monitors `src/` directory
- ๐Ÿ”„ Auto-rebuilds on file changes
- ๐Ÿƒโ€โ™‚๏ธ Automatically runs your app

_Perfect for rapid development cycles_

## โšก Quick Commands

Want to make your life easier? Add these aliases to your `~/.bashrc` or `~/.zshrc`:

```bash
# ๐Ÿ› ๏ธ C++ Development Aliases
alias cpp-start="cmake -G Ninja .. && ninja -j\$(nproc) && ./app"
alias cpp-serve="ninja -j\$(nproc) && ./app"
alias cpp-debug="cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug .. && ninja -j\$(nproc) && ./app"
alias cpp-release="cmake -G Ninja -DCMAKE_BUILD_TYPE=Release .. && ninja -j\$(nproc) && ./app"
alias cpp-clean="ninja clean && ninja -j\$(nproc) && ./app"
alias cpp-watch="while inotifywait -e modify,move,create,delete ../src/; do ninja -j\$(nproc) && ./app; done"
```

**Reload your shell:**

```bash
source ~/.bashrc # or ~/.zshrc
```

**Now you can use:**

- `cpp-start` โ†’ ๐ŸŽฏ Initial build and run
- `cpp-serve` โ†’ โšก Quick rebuild and run
- `cpp-debug` โ†’ ๐Ÿ› Debug build and run
- `cpp-release` โ†’ ๐Ÿš€ Optimized build and run
- `cpp-clean` โ†’ ๐Ÿงน Clean rebuild and run
- `cpp-watch` โ†’ ๐Ÿ‘€ Auto-rebuild on changes

## ๐ŸŽฏ Customizing Your Executable

If your executable isn't named `app`, replace `./app` with your actual name:

**Find your executable name in CMakeLists.txt:**

```cmake
add_executable(my-awesome-app src/main.cpp) # Your exe is "my-awesome-app"
```

**Then use:**

```bash
ninja -j$(nproc) && ./my-awesome-app
```

## ๐Ÿšจ Troubleshooting

โŒ "ninja: command not found"

**Solution:** Install ninja for your platform

```bash
# Ubuntu/Debian
sudo apt install ninja-build

# macOS
brew install ninja
```

โŒ "Permission denied: ./app"

**Solution:**

- โœ… Ensure build completed successfully
- โœ… Check executable exists in build directory
- โœ… Try: `ls -la app` to verify permissions

โŒ CMake configuration fails

**Solution:**

- โœ… Verify `CMakeLists.txt` exists in project root
- โœ… Check all dependencies are installed
- โœ… Try deleting build folder and recreating

โŒ File watching doesn't work

**Solution:**

- โœ… Install `inotify-tools` (Linux)
- โœ… Ensure `../src/` path exists from build directory
- โœ… Check file permissions on src directory

## ๐Ÿค Contributing

1. ๐Ÿด Fork the repository
2. ๐ŸŒฟ Create a feature branch (`git checkout -b amazing-feature`)
3. โœ๏ธ Make your changes
4. ๐Ÿงช Test with both debug and release builds
5. ๐Ÿ“ค Submit a pull request

---

**Made with โค๏ธ and lots of โ˜•**

_Happy Coding! ๐Ÿš€_